Custom query using categories not working

Forums › Forums › Search & Filter Pro › Custom query using categories not working

Viewing 5 posts – 1 through 5 (of 5 total)
  • Anonymous
    #240534

    Hi Trevor,

    My name is Lluc Pallejà, last summer you helped me via Skype to figure out a problem with a custom query and you were really helpful.

    I’m currently working on another project and I know I’m very close.. I can’t get the filter to work.

    Basically I have a bunch of posts, categories and tags:

    I created “A”, “B” ,”C”, “E”.. all the alphabet under a parent category called “letters”. Then I also created 2 more categories, “Spain” and “Not Spain”. The alphabet categories are used to assign posts with the letter of the alphabet and the Spain and Not Spain to determine its origin.

    Then I have a bunch of tags to assign the Genre (Rock, Blues, Jazz, etc.)

    So for example:

    Artist | Origin | Genre

    Madonna | Not Spain | Pop (and this post is assigned with subcategory “M” because its name starts with M)

    So with that in mind I built a custom query like this:

    $lang_id = ($lang == 'en') ? '65' : '20'; 
    
    $category = get_categories(array(
    	'hide_empty' => true,
    	'order' => 'ASC',
    	'orderby' => 'name',
    	'parent' => pll_get_term(236), //this is to retrieve the parent category (using polylang) 
    )); ?>
    
    <div class="filter">
        <?php echo do_shortcode("[searchandfilter id=\"$lang_id\"]"); ?>
    </div>
    
    <?php 
        foreach($category as $cat) :
            $args = array(
                'post_type' => 'post',
                'posts_status' => 'publish',
                'order' => 'ASC',
                'order_by' => 'name',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'category',
                        'field' => 'term_id',
                        'terms' => $cat->term_id,
                    )
                ),
                'search_filter_id' => $lang_id
            );
            $artists = new WP_Query($args); ?>
                <div class="artists container grid-x">
                    <div class="category large-12 small-12 cell">
                        <h4 class="<?php echo $cat->cat_name; ?>"><?php echo $cat->cat_name; ?></h4>
                        <div class="large-12 small-12 cell">	
                            <?php if($artists->have_posts()) : ?>	
                                <?php while($artists->have_posts()) : $artists->the_post(); ?>
                                    <?php if(get_field('featured_artist')) : ?>
                                        <h2 class="featured-artist"><?php the_title(); ?></h2>
                                    <?php else: ?>
                                        <h2><?php the_title(); ?></h2>
                                    <?php endif; ?>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <p><?php pll_e('No artist has been found.', 'tsmgoa'); ?></p>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            <div class="result"></div>
        <?php endforeach; ?>
        <?php wp_reset_query(); ?>
        <?php wp_reset_postdata(); ?>

    This code above works perfectly and displays all posts by letter when I load/refresh the page. Effectively what this code does is, first to pull all the subcategories from “Letters” parent category, then I iterate those subcategories and then I create a custom query with pulling all the posts from those categories and then displaying them accordingly.. all this works perfectly.

    The problem comes when I interact with the Search Filter Pro.

    As you can see I added the parameter “‘search_filter_id’ => $lang_id” to attach S&FPro to the query I created a Search & filter Form as follows:

    Field Relationship “AND”

    Enable Autocount

    – Category (to filter by origin -> Spain, Not Spain and Uncategorized)
    – Tag (to filter by Genre -> Rock, Blues, Pop etc..)
    – Search (Free search)
    – Reset Button (to reset the query)

    Tags, Categories & Taxonomies

    Categories
    -Include (Spain, Not Spain, Uncategorized)

    I don’t want to include all the letter subcategories (A, B, C etc..) because I don’t want them in the filter, I just want to filter by Origin..

    I tried to make simplify by just querying all the posts without the “letters” and this way the Filter Search Pro works perfectly.. so I know there is something wrong with my query and the Search Filter Pro form bu I can’t figure our what exactly..

    I just wonder if we could have a Skype call as we did last year, so I can show it to you and I’ll be easier and faster to understand.

    I’m based in Barcelona.

    Thanks!
    Lluc Pallejà

    Anonymous
    #240539

    Ok, it turns out that my query is correct, and turning the AJAX off from Filter Search Pro make it work properly. At least I know that has something to do with AJAX.

    For the moment I enabled auto submit but I would really find out why it’s not working with AJAX activated.

    Thanks.

    Trevor
    #240603

    Are you able to send me a live link/URL to your search page so I can take a look?

    Anonymous
    #240607

    Hi Trevor,

    I finally managed to make it work even with AJAX, so everthying is working as expected.

    Sorry for not telling you before.

    Thanks!
    Lluc

    Trevor
    #240612

    Ah, OK, no problem. Thanks for getting back to me. I will close this thread for now.

Viewing 5 posts – 1 through 5 (of 5 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.