$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Query child posts with tax query on parents|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Query child posts with tax query on parents

matteradmin7PV0评论

I have this WP_Query:

                new WP_Query(
                        array(
                            'post_type' => 'product_variation',
                            'post_status'       => 'publish',
                            'posts_per_page'    => -1,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => array( 'archive', 'membership' ),
                                    'operator' => 'NOT IN',
                                ),
                            ),
                            'fields' => 'ids',
                        )
                    );

The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.

I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.

Thanks!

I have this WP_Query:

                new WP_Query(
                        array(
                            'post_type' => 'product_variation',
                            'post_status'       => 'publish',
                            'posts_per_page'    => -1,
                            'tax_query' => array(
                                array(
                                    'taxonomy' => 'product_cat',
                                    'field'    => 'slug',
                                    'terms'    => array( 'archive', 'membership' ),
                                    'operator' => 'NOT IN',
                                ),
                            ),
                            'fields' => 'ids',
                        )
                    );

The problem is, that the product_cat taxonomy is set on the parent products, not the variations. Meaning, I need to get all the variations whose parents are not in the specified categories, or in other words: get child posts but run the tax query on the parent posts.

I know this can be achieved with MySQL, but I was wondering if there's a clean way of doing it with WP_Query.

Thanks!

Share Improve this question asked Nov 14, 2018 at 10:27 Reuven KarasikReuven Karasik 1351 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

My solution to this ended up splitting this into 2 queries:

  1. Get the IDs of all the parent products I want to ignore
  2. Get all the variations that don't a post_parent from that list of IDs

    $archived_products = wc_get_products(
        array(
            'type' => array( 'variable', 'variable-subscription' ),
            'paginate' => false,
            'limit' => -1,
            'category' => array( 'archive', 'membership' ),
            'return' => 'ids',
        )
    );
    $my_query->set( 'post_parent__not_in', $archived_products );
    
Post a comment

comment list (0)

  1. No comments so far