0

I have set up a WP_Query within my archive.php page. This is so I can do different things such as pagination etc. I also have two drop down filters categories and archive (yearly).

When selecting a year I am taken to a page with the following URL www.mysite.com/2018/ and the pagination also works www.mysite.com/2018/page/2/. However when filtering the site by year its showing posts from all years and not the selected year.

Here is my WP_query code:

<?php
    $currentCategory = single_cat_title("", false);

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
        'category_name' => $currentCategory,
        'paged'=> $paged,
        'posts_per_page' => '9'
    );
    $query = new WP_Query( $args );

    $tax = 'category';
    $terms = get_terms( $tax );
    $count = count( $terms );

    if ( $count > 0 ): ?>
    <div class="row">
        <div class="col-md-12">
            <?php if (is_date()) {?>
                <h1 class="pagetitle"><?php the_time('Y'); ?></h1>
            <?php } else {?>
                <h1 class=""><?php echo $currentCategory?></h1>
            <?php } ?>
        </div>
    </div>  
    <div class="clearfix">
        <div class="select-filter-wrapper float-left">
            <div class="clearfix">
                <p class="p-filter float-left">Categories:</p>
                <div id="category-filter" class="current-filter float-left">
                    <div id="category-select" class="select-option">
                        <?php if (is_date()) {?>
                            <div id="category-select" class="select-option"><p>All</p></div>
                        <?php } else {?>
                            <p><?php echo $currentCategory?></p>
                        <?php } ?>

                    </div>

                    <ul id="category-list" class="select-dropdown">
                        <li class="cat-item cat-item-all">
                            <a href="<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">All</a>
                        </li>
                        <?php
                        $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
                        $currentTerm = $term->term_taxonomy_id;
                        wp_list_categories( array(
                            'orderby'            => 'description',
                            'order'              => 'ASC',
                            'style'              => 'list',
                            'title_li'           => __( '' ),
                            'taxonomy' => 'category',
                            'walker'             => null,
                            'current_category' => ( isset($currentTerm) ) ? $currentTerm : null
                        ));
                        ?>
                    </ul>
                </div>
            </div>
        </div>

        <div class="select-filter-wrapper float-left">
            <div class="clearfix">
                <p class="p-filter float-left">Archive:</p>
                <div id="category-filter" class="current-filter float-left">
                    <div id="category-select" class="select-option">
                        <?php if (is_date()) {?>
                            <p><?php the_time('Y'); ?></p>
                        <?php } else {?>
                            <p>Year</p>
                        <?php } ?>

                    </div>

                    <ul id="category-list" class="select-dropdown">
                        <!--<li class="current-cat"><a href="<//?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>">All</a></li>-->
                        <?php $args = array(
                            'type'            => 'yearly',
                            'limit'           => '',
                            'format'          => 'html', 
                            'before'          => '',
                            'after'           => '',
                            'show_post_count' => false,
                            'echo'            => 1,
                            'order'           => 'DESC',
                            'post_type'     => 'post'
                        );

                          wp_get_archives( $args ); 

                        ?>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <?php endif;
    if ( $query->have_posts() ): ?>
    <div class="row row-eq-height">
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <div class="col-md-4 o-mg-btm-md">
            <?php get_template_part( 'template-parts/content', get_post_type() );?>
        </div>
        <?php endwhile; ?>
    </div>

    <?php wordpress_numeric_post_nav(); ?>

    <?php endif; ?>

Do I need add something else within my $args to get the yearly filtering to work?

1

You will have to pass the year in the args for example:

$args = array(
  'year'  => '2012',
);
| improve this answer | |

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.