-1

I'm trying to get custom archive posts and default posts combined onto one archive page which I have managed... sort of but I can't get pagination to work either using the pagenavi plugin or the themes default pagination code. I have tried using paged but if anything did show I'm then not sure it would page both categories together or just one.

I need to combine two categories, one a custom taxonomy, the other the default and then have them styled differently and have a set number of the combined posts on the page and to be able have them in pages of 9 posts.

code I'm using:

            <?php if (is_category()) { ?>
                <h1 class="page-title">
                <span><?php _e(); ?></span> <?php single_cat_title(); ?>
                </h1>
            <?php } ?>
                <ul>
                    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <?php get_template_part( 'partials/loop', 'archive' ); ?>
                    <?php endwhile; ?>
                </ul>

            <?php rewind_posts(); ?>

            <?php
            $args = array(
            'post_type' => 'events',
            'posts_per_page' => '6',
            'paged' => $paged,
            'tax_query' => array(
            array(
            'taxonomy' => 'event_cat','events',
            'field' => 'slug',
            'terms' => 'events','upcoming-events'
            )
            )
            );?>
            <?php $the_query = new WP_Query( $args ); ?>
                <ul>
                    <?php if ( $the_query->have_posts() ) :
                    while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                    <?php get_template_part( 'partials/loop', 'archive-events' ); ?>
                    <?php endwhile; ?>
                </ul>

            <?php wp_pagenavi(); ?>

            <?php else : ?>
            <?php get_template_part( 'partials/content', 'missing' ); ?>
            <?php endif; ?>

Changed the above to:

                        <?php if (is_category()) { ?>

                        <h1 class="page-title">
                            <span><?php _e(); ?></span> <?php single_cat_title(); ?>
                        </h1>
                    <?php } ?>

                        <ul class="small-block-grid-1 medium-block-grid-2 large-block-grid-3">

                            <?php $query = new WP_Query( array( 'cat' => '7' )); ?>


                            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                            <?php get_template_part( 'partials/loop', 'archive' ); ?>
                            <?php endwhile; ?>
                        </ul>

                        <?php else : ?>
                        <?php get_template_part( 'partials/content', 'missing' ); ?>
                        <?php endif; ?>

                        <?php rewind_posts(); ?>

                 <?php

                $args = array(
                'post_type' => 'events',
                'posts_per_page' => '3',
                'paged' => get_query_var( 'paged' ),
                'tax_query' => array(
                array(
                'taxonomy' => 'event_cat',
                'field' => 'slug',
                'terms' => 'events'
                )
                )
                );?>

                <?php $the_query = new WP_Query( $args ); ?>

                        <ul class="small-block-grid-1 medium-block-grid-2 large-block-grid-3">
                            <?php if ( $the_query->have_posts() ) :
                            while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                            <?php get_template_part( 'partials/loop', 'archive-events' ); ?>
                            <?php endwhile; ?>
                        </ul>
                        <?php if (function_exists('joints_page_navi')) { ?>
                        <?php wp_pagenavi(); ?>
                        <?php } else { ?>
                        <nav class="wp-prev-next">
                            <ul class="clearfix">
                                <li class="prev-link"><?php next_posts_link(__('&laquo; Older Entries', "jointstheme")) ?></li>
                                <li class="next-link"><?php previous_posts_link(__('Newer Entries &raquo;', "jointstheme")) ?></li>
                            </ul>
                        </nav>
                        <?php } ?>

                        <?php endif; ?>
  • It is a bit difficult to understand you here. Concern 1: a) Which is the custom taxonomy and b) which is the term assigned to this custom taxonomy. Concern 2: a) Which is the built-in category. Your tax_query is all wrong if you are trying to get posts from a custom taxonomy and a built-in category – Pieter Goosen Aug 30 '14 at 10:11
  • Also, please advice on which page are you doing this – Pieter Goosen Aug 30 '14 at 10:14
  • Doing this on the archive.php file. Added code to the above that now works for me but it's probably still a bit messy? – Nsokyi Aug 31 '14 at 13:08
0

I would suggest that you scrap everything that you have and go back to the default loop on your archive page. Custom queries are always troublesome on archive pages.

Use pre_get_posts to alter the query vars before the main query is executed. This way, you won't have any unnecessary queries and problems with pagination. You are going to make use of a tax-query here to include your two terms. For all available parameters for use in pre_get_posts, see WP_Query

Here is my idea

  • As said, return to the default loop in your archive page

  • In your functions.php, add the following using pre_get_posts and the conditional check is_archive(). This code will add the two desired terms to your archive page

    function custom_archive_query( $query ) {
        if ( !is_admin() && $query->is_archive() && $query->is_main_query() ) {
            $query->set( 'posts_per_page', 9 );
            $query->set( 'post_type', 'any' );
            $tax_query = array(
                'relation' => 'OR',
                array(
                    'taxonomy' => 'event_cat',
                    'field'    => 'slug',
                    'terms'    => 'events',
                ),
                array(
                    'taxonomy' => 'category',
                    'field'    => 'term_id',
                    'terms'    => '7',
                ),
            );
             $query->set( 'tax_query', $tax_query );
    
        }
    }
    add_action( 'pre_get_posts', 'custom_archive_query' );
    
  • As for the styling issue, on your archive page inside the loop, use the two conditionals, has_category() and has_term to check to which term a post belongs, and according to that, style it

    if( has_category( 7 ) ) {
        // DO SOMETHING FOR CATEGORY
    }elseif( has_term( 'events', 'event_cat' ) ) {
        // DO SOMETHING FOR TERM event_cat
    }
    

PLEASE NOTE: This is all untested

EDIT

You can do the exact same thing as above using WP_Query as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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