8

Simple query but for some reason is not displaying the correct posts, trying to display a post with the monthly-to-do-list term, if no results then display a post with the community-events' term. Any suggestions?

$todo_args = array(
   'cat' => $my_category_id,
   'posts_per_page' => 1,
   'tax_query' => array(
        'relation' => 'OR',
            array(
               'taxonomy' => 'postkicker',
                'field'    => 'slug',
                'terms'    => 'monthly-to-do-list',
                ),
            array(
                'taxonomy' => 'postkicker',
                'field'    => 'slug',
                'terms'    => 'community-events',
                ),
            ),
    'orderby' => 'date',
    'order' => 'DESC'
);
Jose Salazar
  • 341
  • 1
  • 4
  • 12

2 Answers2

15

try adding array and make it like this :

$todo_args = array(
   'cat' => $my_category_id,
   'posts_per_page' => 1,
   'tax_query' => array(
        'relation' => 'OR',
            array(
               'taxonomy' => 'postkicker',
                'field'    => 'slug',
                'terms'    => array('monthly-to-do-list'),
                ),
            array(
                'taxonomy' => 'postkicker',
                'field'    => 'slug',
                'terms'    => array('community-events'),
                ),
            ),
    'orderby' => 'date',
    'order' => 'DESC'
);

As you may notice terms is plural so you may also simplify your query like this :

$todo_args = array(
   'cat' => $my_category_id,
   'posts_per_page' => 1,
   'tax_query' => array(
            array(
               'taxonomy' => 'postkicker',
                'field'    => 'slug',
                'terms'    => array('monthly-to-do-list','community-events'),
                ),
            ),
    'orderby' => 'date',
    'order' => 'DESC'
);
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
0

As above BUT you have to include relation as well.

$todo_args = array(
   'cat' => $my_category_id,
   'posts_per_page' => 1,
   'tax_query' => array(
       'relation' => 'OR',
       array(
          'taxonomy' => 'postkicker',
          'field'    => 'slug',
          'terms'    => array('monthly-to-do-list','community-events'),
       ),
   ),
   'orderby' => 'date',
   'order' => 'DESC'
);