Home > Software design >  Wordpress array for product and post on same page
Wordpress array for product and post on same page

Time:11-20

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

CodePudding user response:

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);
  • Related