Custom $wp_query, pagination issue

  • Unknown's avatar

    I use the following code to implement wp_dropdown_categories. As a result, the desired array built from the drop-down menu is displayed. When the result is not collected on one page, the arrows to scroll through the pages are displayed, which work correctly. When select another category from the drop-down menu, the required result is loaded into $ wp_query. But the current page number does not change (does not load the first ), it remains the one generated in the previous result. As a result of this problem, when the new filtering result is smaller (for example: it adds up to two pages) and the current one is a third page, the displayed result is empty. For some reason, after selecting another item from the drop-down menu, the page number is not reset to the first.

    In functions.php

    function km_get_tools_in_taxonomy_term_tax_query() {

    $selected_term = km_get_selected_taxonomy_dropdown_term();

    // If a term has been selected, use that in the taxonomy query.
    if ( $selected_term ) {
    return array(
    array(
    ‘taxonomy’ => ‘category’,
    ‘field’ => ‘term_id’,
    ‘terms’ => $selected_term,
    ),
    );
    }
    }

    function km_get_selected_taxonomy_dropdown_term() {
    return isset( $_GET[ ‘category’ ] ) && $_GET[ ‘category’ ] ? sanitize_text_field( $_GET[ ‘category’ ] ) : ”;
    }

    In archive.php

    <?php
    global $wp_query, $paged;
    $paged = get_query_var( ‘paged’ ) ? get_query_var( ‘paged’ ) : 1;
    $temp = new WP_Query();
    $temp = $wp_query;
    $wp_query = null;

    $args = array (
    ‘post_type’ => ‘post’,
    ‘paged’ => $paged,
    ‘posts_per_page’ => 6,
    ‘tax_query’ => km_get_tools_in_taxonomy_term_tax_query(),
    );
    $wp_query = new WP_Query();
    $wp_query->query($args);
    ?>

    <div class=”tools-dropdown”>

    <form id=”tool-category-select” class=”tool-category-select” method=”get”>

    <?php
    // Create and display the dropdown menu.
    wp_dropdown_categories(
    array(
    ‘orderby’ => ‘NAME’,
    ‘taxonomy’ => ‘category’,
    ‘name’ => ‘category’,
    ‘show_option_all’ => ‘All category’,
    ‘selected’ => km_get_selected_taxonomy_dropdown_term(),
    ‘show_count’ => ‘1’
    ) );?>
    <input type=”submit” value=”View” />
    </form>
    </div>
    <?php if ( have_posts() ) : ?>
    <div class=”blog-list container”>
    <?php
    $separator = 0;
    $separator_2 = 1;

    while ( have_posts() ) : the_post();
    get_template_part( ‘home-content/home’, get_post_format() );

    if($separator++ == 2){
    echo ‘<span class=”clear clear-three”></span>’;
    $separator = 0;
    }
    if($separator_2%2 == 0){
    echo ‘<span class=”clearfix clear-two”></span>’;

    }
    $separator_2++;

    endwhile; ?>

    <div class=”clear”></div>
    </div>

    <div class=”nav-arrows text-center”>
    <div class=”left-arrow”>
    <?php next_posts_link( __(‘Older Postsssss’) . ” <span class=’fa fa-long-arrow-right’></span>”); ?>
    </div>
    <div class=”right-arrow”>
    <?php previous_posts_link(“<span class=’fa fa-long-arrow-left’></span> “. __(‘Newer Posts’) .””); ?>
    </div>
    <span class=”clear”></span>
    </div>

    <?php endif;
    $wp_query = null;
    $wp_query = $temp;
    wp_reset_postdata();
    wp_reset_query();?>

  • Hello there,

    Many thanks for reaching out.

    Are you able to confirm the URL of the website that you need assistance with please?

  • The topic ‘Custom $wp_query, pagination issue’ is closed to new replies.