$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Filter posts in category archive page by year using a dropdown|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Filter posts in category archive page by year using a dropdown

matteradmin10PV0评论

I'm trying to filter my posts by year in different categories by changing the query using a dropdown with this code:

                <form method="post" action="">
                    <select id="year" name="year" onchange='this.form.submit();'>
                        <option value="2019">2019</option>
                        <option value="2018">2018</option>
                        <option value="2017">2017</option>
                        <option value="2016">2016</option>
                        <option value="2015">2015</option>
                    </select>
                </form>

                <div id="categories">
        <?php wp_dropdown_categories( 'show_option_none=Select category' ); ?>
        <script type="text/javascript">
            <!--
            var dropdown = document.getElementById("cat");
            function onCatChange() {
                if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                    location.href = "<?php if(pll_current_language == "en"){
                        echo esc_url( home_url( '/' ) ); } else { echo esc_url('beta.dev/');} ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
                }
            }
            dropdown.onchange = onCatChange;
            -->
        </script>
    </div>

    <ul>

    <?php query_posts('year=' . $_POST['year'] ); ?>
    <?php if (have_posts() ) : while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <li><p class="post-info"><?php the_date(); ?> | <?php $categories = get_the_category();

     if ( ! empty( $categories ) ) {
         echo esc_html( $categories[0]->name );   
     } ?></p></li>
    <?php 
    endwhile; else :
    ?>
        <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
        <?php endif; wp_reset_postdata() ?>
    </ul>

    </div>
    </section>
    </div>
    </div>
    <?php get_footer(); ?>

The category part is working properly, and shows posts from the chosen category. However, changing the year instead redirects me to my custom index.php in my template, can someone point me in the right direction? Would love some feedback/help!

Regards

Post a comment

comment list (0)

  1. No comments so far