• Resolved El Pablo

    (@el-pablo)


    I have made a category filter for a custom post type, via an ajax request. The ajax is working, but I still receive all the result back. So basically, my query category filter is not working, and I would like to know why?

    The proper category name is fetched correctly. I’ve logged it in the console. So it must be something with my wp query.

    <?php
    
    $category = $_GET['category'];
    
    $args = array(
        'post_type' => 'mixtapes',
        'posts_per_page' => -1,
        'orderby' => 'date',
        'order' => 'DESC',
        'tax_query' => array(
            'taxonomy' => 'mixtapes_categorie',
            'field'    => 'slug',
            'terms' => $category
        )
    );
    
    $mixtapes = new WP_Query($args);
    
    if ($mixtapes->have_posts()) :
        while ($mixtapes->have_posts()) :
            $mixtapes->the_post(); ?>
    
            <div class="col-md-6">
                <?php the_content(); ?>
            </div>
    
    <? endwhile;
    endif; ?>

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Your tax_query arg needs to be nested in another array, like so:

        'tax_query' => array( array(
            'taxonomy' => 'mixtapes_categorie',
            'field'    => 'slug',
            'terms' => $category,
        ),),

    This need seems weird in this context. It makes more sense when there are multiple taxonomy criteria to address.

    FWIW, it’s useful to include a dangling comma after the last array element declaration like I have done in the above example. It makes the code easier to maintain when array elements need to be added or removed. This is only valid in PHP arrays. Doing so elsewhere likely results in syntax errors. Entirely optional of course.

    Thread Starter El Pablo

    (@el-pablo)

    @bcworkz thanks, it works now

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP Query filter not working’ is closed to new replies.