简体   繁体   中英

Wordpress category & taxonomy loop with pagination

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?

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 eg 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM