woocommerce 为子产品页面获取分组产品“父”

时间:2021-06-27 15:11:56

标签: wordpress woocommerce

我试图有一个部分来显示那些链接的子产品的父产品。这样用户就可以知道从哪里返回到分组的产品。

但是我不知道查询应该如何工作。下面的代码获取了所有父产品,我只想显示链接到子产品的那个。

<?php
function repeater_dynamic_query( $query ) {
  global $post;
  if ( $query->query['post_type'][0] == 'product' ) {
    $query->set( 'tax_query', array(
                                array(
                                  'taxonomy' => 'product_type',
                                  'field' => 'slug', 
                                  'terms' => grouped                               
                                )
                              ) );
    $query->set( 'orderby', 'rand' );
    $query->set( 'post__not_in', array($post->ID) );
    $query->set( 'no_found_rows', true );
  }
}
add_action( 'pre_get_posts', 'repeater_dynamic_query' );
?>

我从 woo doc 中找到了函数,但它似乎已过期。不确定这是否有帮助。谢谢

get_parent()

https://woocommerce.github.io/code-reference/classes/WC-Product-Grouped.html

1 个答案:

答案 0 :(得分:0)

所以这就是我找到的人设法解决了我的问题。对于那些可能需要这个的人。

add_action( 'wp_head', function() {
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'no_found_rows' => 1,
        'meta_query' => [ 
            array(
                'key' => '_children',
                'compare' => 'LIKE',
                'value' => get_the_ID()
            ),
        ]
    );

    $query = new WP_Query( $args );
    if( $query->have_posts() ) {
        print_r($query);
    }
});
相关问题