3

I have two taxonomies belonging to the same post type (posts).

Adventure - belonging to the category taxonomy Adventure - belonging to the holiday-type taxonomy

I want to run two queries:

$args = array(
            'post_type' => 'post',
            'tax_query' => array(
            'relation' => 'OR',
                array(
                    'taxonomy' => 'holiday-types',
                    'field'    => 'slug',
                    'terms'    => 'adventure',
                ),
                array(
                    'taxonomy' => 'category',
                    'field'    => 'slug',
                    'terms' => 'adventure',
                ),  
            ),

        );
        $the_query = new WP_Query( $args );

Even though the terms are the same they have different posts associated to them.

This query only returns posts with the holiday-types taxonomy and not the category taxonomy too. If I comment out the array with the holiday types, then the taxonomy category shows the relevant posts.

I need them to both show together as well as with pagination.

'paged' => get_query_var('paged')

I have read that i need to add this to the first array but i guess i cant test this until i get the loop working correctly first.

Any ideas where I am going wrong please?


Ok. I have got somewhere with this now but the pagination doesn't work. It goes to a 404 page when clicking through to the other pages e.g. 2, 3, 4. What is it I am missing?

global $query_string;
$myquery = wp_parse_args($query_string);
$myquery = array(
    'paged' => $paged,
    'numberposts'=>-1,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'terms' => array('adventure'),
            'field' => 'slug',

        ),
        array(
            'taxonomy' => 'holiday-types',
            'terms' => array('adventure'),
            'field' => 'slug',
        ),        
    ),
 );
query_posts($myquery);

while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_pagenavi();
wp_reset_query();   
3
  • your codes seems perfectly okay too me. I hope somebody soon help u out, all the best\
    – terminator
    Commented Sep 2, 2015 at 11:15
  • 1
    please show SQL of your $the_query obj by var_dump($the_query->request)
    – Ignat B.
    Commented Sep 2, 2015 at 11:25
  • 1
    By default the results are paginated according to the setting in your Reading Settings for "Blog pages show at most", if you haven't set up pagination yet are you sure it's not just that the first N results are from the Holiday Types taxonomy?
    – totels
    Commented Sep 2, 2015 at 15:15

1 Answer 1

1

Don't use 'numberposts'=>-1 because numberposts is depracated moreover -1 shows all the post on one page.

Instead use posts_per_page and set its value according to how many posts do you want.

$paged= (get_query_var('paged' )) ? get_query_var('paged'):1; 
global $query_string;
$myquery = wp_parse_args($query_string);
$myquery = array(
    'paged' => $paged,
    'posts_per_page'=>10,
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'terms' => array('adventure'),
            'field' => 'slug',

        ),
        array(
            'taxonomy' => 'holiday-types',
            'terms' => array('adventure'),
            'field' => 'slug',
        ),        
    ),
 );
query_posts($myquery);

while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_pagenavi();
wp_reset_query(); 
2
  • numberposts is not depreciated, but it is an invalid parameter for query_posts (which you should really not use), numberposts is used with get_posts but does get converted to posts_per_page before being passed to WP_Query Commented Sep 3, 2015 at 5:58
  • @Pieter Goosen thanks for the valuable informatiom
    – terminator
    Commented Sep 3, 2015 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.