4

I would like to get results from two post types,

1) Post, only show 'business' kicker 2) local-news, show all kickers

I've so far:

$args=array(
'cat' => $my_category_id,
'post_status' => 'publish',
'posts_per_page' => 5,
'post_type' => array('post', 'local-news'),
'tax_query' => array(
      'relation' => 'OR',
   array(
     'taxonomy' => 'postkicker',
      'term' => 'business'
   ),
    array(
     'taxonomy' => 'impactkicker',
   ),
 ),
'orderby'    => 'date',
'order'      => 'DESC'
);

Currently is not showing both post types, any suggestions? Thanks in advance

Jose Salazar
  • 341
  • 1
  • 4
  • 12
  • Does it work if you try without the taxonomy query? – mcon Sep 29 '16 at 21:18
  • Do you have a real need the `cat` argument? I have a feeling that its conflicting with your taxonomy query. – The Maniac Sep 29 '16 at 21:20
  • @mcon Actually is not working without the tax_query. Is that the right way to show multiple post_types? – Jose Salazar Sep 29 '16 at 21:25
  • That's the right way to show multiple post_types **but we don't know how you registered** the post type `local-news`, so it may be that you've registered it in a way that isn't referenced as `local-news`, but something else. Show us your `register_post_type` code. – random_user_name Sep 29 '16 at 22:24

1 Answers1

1

You need to make sure you've connected your cpt local-news to category taxonomy as well, because you are trying to select by this taxonomy. Here's extensively commented approach which will do what you need i guess. At least if I got your idea clearly. You can change tax_query main relation to OR instead of AND to output items if they don't have category set as well.

$args = array(
    // 'cat' => $my_category_id, // better  replace it with category in tax_query, see below.
    'post_status'    => 'publish',
    'posts_per_page' => 5,
    'post_type'      => array( 'post', 'local-news' ),
    'tax_query'      => array(
        'relation' => 'AND', // we set it to AND because we want all posts of this category i guess.
        array(
            'taxonomy' => 'category',
            'term'     => $my_category_id,
            'field'    => 'term_id',
            'operator' => 'IN', // just to be more explicit.
        ),
        array( // we create nested sub queries which will filter by other 2 taxonomies, which in turn has OR relation.
            'relation' => 'OR',
            array(
                'taxonomy' => 'postkicker',
                'field'    => 'slug',    // by default it's term_id and you are passing in a slug so set it explicitly.
                'term'     => 'business',
                'operator' => 'IN', // just to be more explicit.
            ),
            array(
                'taxonomy' => 'impactkicker',
                'field'    => 'slug',           // set these or not add rule for taxonomy at all.
                'term'     => 'your-term-slug', // same here.
                'operator' => 'IN', // it's a default value, but ou can set 'EXISTS' if you need to check if whatever term of such taxonomy is assigned.
            ),
        ),
    ),
    'orderby'        => 'date',
    'order'          => 'DESC',
);
Mikhail.root
  • 764
  • 9
  • 18