A
A
Alexander Osadchy2020-08-10 10:23:55
WordPress
Alexander Osadchy, 2020-08-10 10:23:55

How to fix load more in WP?

I'm doing ajax post filters + the "Show more" button according to this instruction

. The filters work fine, but the load more button creates a problem.

If I filtered records by categories
5f2aa6a30dd30077099183.png

Then, as planned, the "load more" button should load records from the filtered categories. And she loads all the records.

I know that the categories filtered by the array should be passed here:

function misha_loadmore_ajax_handler(){
  ...
  $args['category_name'] = 'services, categories';
  ...
}

But I don't know how to do it right.

PS The category filters themselves in the template (and I have two filters) are displayed like this:
<?php
        $cat_services = get_category_by_slug('services');
        $categories_services = get_categories( [
            'taxonomy'     => 'category',
            'type'         => 'post',
            'parent'       => $cat_services->term_id,
            'hide_empty'   => 0,
        ] );

        echo '<h3>'. $cat_services->cat_name .'</h3>';

        if( $categories_services ){
            echo '<ul class="cat-check-list" id="categoryfilter-1" name="categoryfilter-1">';
            foreach( $categories_services as $cat ){
                echo '<li><div class="checkbox-custom"><span><label for="category_'. $cat->cat_ID .'"><input type="checkbox" id="category_'. $cat->cat_ID .'" name="category_'. $cat->cat_ID .'" />' . $cat->name . '</label></span></div></li>';
            }
            echo '</ul>';
        }


        $cat_categories = get_category_by_slug('categories');
        $categories_categories = get_categories( [
            'taxonomy'     => 'category',
            'type'         => 'post',
            'parent'       => $cat_categories->term_id,
            'hide_empty'   => 0,
        ] );

        echo '<h3>'. $cat_categories->cat_name .'</h3>';
        if( $categories_categories ){
            echo '<ul class="cat-check-list" id="categoryfilter-2" name="categoryfilter-2">';
            foreach( $categories_categories as $cat ){
                echo '<li><div class="checkbox-custom"><span><label for="category_'. $cat->cat_ID .'"><input type="checkbox" id="category_'. $cat->cat_ID .'" name="category_'. $cat->cat_ID .'" />' . $cat->name . '</label></span></div></li>';
            }
            echo '</ul>';
        }
  ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2020-08-11
@DELUX

On the client side, collect the selected categories and transfer to the server

data : {
      'action': 'loadmore', // the parameter for admin-ajax.php
      'query': misha_loadmore_params.posts, // loop parameters passed by wp_localize_script()
      'page' : misha_loadmore_params.current_page // current page
      'categories': ...
    },

Accept this parameter on the server
$args['category_name'] = $_POST['categories']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question