0

Got working ajax filters and working pagination. But they working only if one of em disabled.

Just dont know how to save working pagination without clearing ajax filter.

Here is html code for form

    <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">
      <div class="sort_wrap">
        <div class="radio_fltr">
            <input type="radio" name="price_o" id="asc" value="ASC"/>
            <label for="asc">ASC</label>
        </div>
        <div class="radio_fltr">
            <input type="radio" name="price_o" id="desc" value="DESC"/> 
            <label for="desc">DESC</label>
        </div>
      </div>
      <input type="hidden" name="action" value="myfilter">
    </form>

Here is js snippet for ajax filtering

$('input[type="radio"][name="price_o"]').change(function(){
    var filter = $('#filter');
    $.ajax({
        url:filter.attr('action'), 
        data:filter.serialize(), 
        type:filter.attr('method'), 
        success:function(data){ 
            $('#response').html(data);
        }
    });
    return false;
});

And functions.php function for catching asc/desc order.

function true_filter_function(){
    $args = array(
        'orderby' => 'meta_value_num',
        'meta_key' => 'price',
        'order' => $_POST['price_o']
    );

    $query = new WP_Query( $args );

    if( $query->have_posts() ) :
        while( $query->have_posts() ): $query->the_post();
            echo '<h2>' . $query->post->post_title . '</h2>';
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    die();
}

add_action('wp_ajax_myfilter', 'true_filter_function');
add_action('wp_ajax_nopriv_myfilter', 'true_filter_function');

I know i can add

  $current_page = (get_query_var('paged')) ? get_query_var('paged') : 1;

'paged' variable inside $args massive and at least add paginate_links() function, event without styling, but if pagination is working and i will try to use my filter, filter is used and clicking on next page will cause page number change visually and in url, but same content is in content area no matter what page u on.

Will be very thankfull for help.

Your Answer

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

Browse other questions tagged or ask your own question.