1

I've used the below tutorial for my WordPress site to add a filter to one of the pages.

https://www.youtube.com/watch?v=KRc47o8uDhk&list=PLSeCxQ7f7r87qbYmqXmOAcvoshw1e6ewQ&index=3

It works well when adding the code as is. However, I need it to instead work with a custom post type 'libraries' and custom taxonomies 'library_types'.

I replaced $categories = get_categories($cat_args); with $categories = get_terms( 'library_types' );, which now shows the correct taxonomies. And also changed 'post-type'=> 'post' to 'libraries'. However it now returns no results. What other variables need to change in order for this to work?

Original Github below.

https://github.com/guyku/WordPressAjax_RyanMcGovern_Lesson

EDIT:

I figured it's probably the filter_ajax that needs adjusting. So I've tried the following:

$category = $_POST['category'];
//$category = get_cat_ID($_POST['library_types']);
//$category = get_post_meta('library_types');
//$category = get_term_meta('library_types');
//$category = get_terms( 'library_types' );

I know it's correctly parsing the ID number because I'm able to echo it through $category, but it's just not displaying any results.

If I use the bottom line instead $category = get_terms( 'library_types' ); then it sort of works but only shows the category items with the highest ID number.

Would appreciate any insight.

Page content:

<!-- Filter -->
        <div class="categories">
            <ul>
            <?php 
            //$cat_args = array(
            //  'exclude' => array(1),
            //  'option_all' => 'All'
            //);

            //$categories = get_categories($cat_args);
            $categories = get_terms( 'library_types' );

            foreach($categories as $cat) : ?>
                <li class="js-filter-item"><a data-category="<?= $cat->term_id; ?>" href="<?= get_category_link($cat->term_id); ?>"><?= $cat->name; ?></a></li>
            <?php endforeach; ?>
            </ul>
        </div>
        
        
        <!-- Display all library post types -->
        <div class="library-wrapper">
            <div class="js-filter">
                <?php

                $args = array(
                    'post_type' => 'libraries',
                    'posts_per_page' => -1
                    );

                    $query = new WP_Query($args);

                    if($query->have_posts()) :
                        while($query->have_posts()) : $query->the_post();
                            the_title('<h2>', '</h2>');
                            get_post_meta('library_types');
                            get_cat_ID('library_types');
                        endwhile;
                    endif;
                    wp_reset_postdata(); ?>
            </div>
        </div>

JS Content:

    function($){

    $(document).ready(function(){
        $(document).on('click', '.js-filter-item > a', function(e){
            e.preventDefault();

            var category_var = $(this).data('category');

            $.ajax({
                url: wp_ajax.ajax_url,
                data: { action: 'filter', category: category_var },
                type: 'post',
                success: function(result) {
                    $('.js-filter').html(result);
                },
                error: function(result) {
                    console.warn(result);
                }
            });
        });
    });

})(jQuery);

Functions content:

    function load_scripts() {
    wp_enqueue_script('ajax', get_theme_file_uri() . '/sorting.js', array('jquery'), NULL, true);
    
    wp_localize_script('ajax' , 'wp_ajax',
        array('ajax_url' => admin_url('admin-ajax.php'))
        );

}

add_action( 'wp_enqueue_scripts', 'load_scripts');


add_action( 'wp_ajax_nopriv_filter', 'filter_ajax' );
add_action( 'wp_ajax_filter', 'filter_ajax' );

function filter_ajax() {


$category = $_POST['category'];
//$category = get_cat_ID($_POST['library_types']);
//$category = get_post_meta('library_types');
//$category = get_term_meta('library_types');
//$category = get_terms( 'library_types' );

$args = array(
        'post_type' => 'libraries',
        'posts_per_page' => -1
        );

        if(isset($category)) {
            $args['category__in'] = array($category);
            echo $category;
        }

        $query = new WP_Query($args);

        if($query->have_posts()) :
            while($query->have_posts()) : $query->the_post();
                the_title('<h2>', '</h2>');
            endwhile;
        endif;
        wp_reset_postdata(); 


    die();
}

1 Answer 1

1

Nevermind, I've managed to solve it. For anyone interested, the problem was 'category__in' isn't support by custom post types so instead needed to edit the $args array to include 'tax_query'.

$args = array(
        'post_type' => 'libraries',
        'tax_query' => array(
            array(
                'taxonomy' => 'library_types',
                'terms'    => $category
            ),
        ),
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.