Forums › Forums › Search & Filter Pro › Search and Filter Pro not working with custom query
Tagged: custom query
- This topic has 9 replies, 3 voices, and was last updated 7 years, 9 months ago by
Anonymous.
-
Anonymous(Private) January 24, 2018 at 9:35 pm #154746
I fixed that, but now just the custom query results appear as they did before, with no search or filter UI appearing.
I also added the shortcode for the results, same thing.
<?php do_shortcode('[searchandfilter id="6128"]') ?> <?php do_shortcode('[searchandfilter id="6128" show="results"]') ?>Trevor(Private) January 25, 2018 at 7:22 am #154777I think I can see what is wrong. You have Custom Post Type and you are using your own template for the archive. If our plugin is disabled, does the archive page for that CPT use that template? If so, did you try and use the Post Type Archive Display Results method?
Also, you can add our query to the arguments for the query by adding this to the arguments array:
$args = array( 'post_type' => 'productions', 'posts_per_page' => 10, 'order' => 'DESC', 'search_filter_id' => 6128, 'meta_query' => array( array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ), ) );Anonymous(Private) January 25, 2018 at 3:14 pm #154903I tried to add the code to the existing query args, and it just returned. There was no UI for filtering the posts by year.
Yes, I have two page templates that display the CPT Productions. The main CPT archive, file called archive-productions.php only displays future productions.
I have another page template called page-past-productions.php that has a custom query that displays only past productions. This is the page where I want to display the filter UI.
Anonymous(Private) January 26, 2018 at 3:41 am #155080Ok, I figured out a way to make it work, but I’m still running into one more issue.
I reset the search form to use display results as a post type archive.
I then created a separate page template that just displays upcoming productions, and am using the archives-productions.php template to display past productions, instead of the other way around.
The filter UI is showing now and the filter works. However, I only want to display past productions, and then filter through those using the plugin.
I tried using the edit query arguments filter, but I can’t seem to get it to work.
Here’s what I added to my functions.php file:
//Modify query args for filter UI on Productions archive template function filter_function_name( $query_args, $sfid ) { //if search form ID = 6128, the do something with this query if($sfid==6128) { //Today's date $today = date('Y-m-d H:i:s'); $args = array( 'meta_query' => array( array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ), ) ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );Ross Moderator(Private) January 26, 2018 at 11:13 am #155099Hi Debbie
There are a couple of issues with the code.
You are setting up
$argsvariable but returning$query_argsso your modifications are not being passed through.Also using
$args =replaces all the data in the existing query, we don’t want to replace, but add your modification.Without checking if your date query actually works, my suggestion would be to modify the code like:
//Modify query args for filter UI on Productions archive template function filter_function_name( $query_args, $sfid ) { //if search form ID = 6128, the do something with this query if($sfid==6128) { //Today's date $today = date('Y-m-d H:i:s'); $meta_query = array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ); $query_args['meta_query'] = array($meta_query); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );Thanks
Ross Moderator(Private) January 26, 2018 at 11:15 am #155101BTW, I’ve just thought, there is a
post metasettings tab in your search form, where you may be able to create conditions like this (but you likely won’t be able to specify a specific time, like in your code. -
AuthorPosts