WordPress array for product and post on same page

WordPress array for product and post on same page

Problem Description:

I use the array to show all products and posts on the same page:

$args = array( 
 'post_type' => array( 'product', 'post' ),
 'posts_per_page' => 999,
 'orderby'   => 'meta_value_num', 
 'meta_key' => 'score_value',
 'order' => 'DESC'
);
 
$custom_query = new WP_Query($args); 

Now I want to filter posts 'cat' => '82,85' and products 'product_cat' => 96.

I have added the code snippes to my code, but after no post or products are shown.

$args = array( 
 'post_type' => array( 'product', 'post' ),
 'cat' => '82,85',
 'product_cat' => 96,
 'posts_per_page' => 999,
 'orderby'   => 'meta_value_num', 
 'meta_key' => 'score_value',
 'order' => 'DESC'
);
 
$custom_query = new WP_Query($args); 

How should I modify my code that both post types and all posts and products within the categories will be displayed?

Thank you,

Mags

Solution – 1

You can do that by using tax_query.

  $args = array(
    'post_type' => array('product', 'post'),
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'term_id',
            'terms'    => ['82', '85'],
        ),
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => ['96'],
        ),
    ),
    'posts_per_page' => 999,
    'orderby'        => 'meta_value_num',
    'meta_key'       => 'score_value',
    'order'          => 'DESC',
);

$custom_query = new WP_Query($args);
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject