Forums › Forums › Search & Filter Pro › Custom query using categories not working
- This topic has 4 replies, 2 voices, and was last updated 6 years, 7 months ago by
Trevor.
-
Anonymous(Private) April 19, 2020 at 11:06 am #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(Private) April 19, 2020 at 1:17 pm #240539Ok, 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.
-
AuthorPosts