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 6 years, 10 months ago by Anonymous.
-
Anonymous(Private) January 24, 2018 at 5:45 pm #154499
I’m having a difficult time getting the plugin to work with a custom post type on a page where I have a custom query built.
I have a CPT called Productions. The upcoming productions and the past productions are displayed in different areas of the site. The archive-productions.php template only displays upcoming productions.
I have a page template that displays only the past productions, called page-past-productions.php, using a custom query.
I’m trying to implement a filter UI in the past-productions page template so users can drill down by year.
I installed the plugin and created the form, using the post meta of “event date,” which corresponds to the ACF date picker field I’m using for Productions CPT.
I tried to implement the form using the shortcode, and I got a syntax error.
Parse error: syntax error, unexpected '6128' (T_LNUMBER) in /Users/labedzde/Documents/Sites/acmusic/wp-content/themes/acm/page-past-productions.php on line 18
If it helps, here’s the whole code of my custom query, which sits in a page template called page-past-productions.php):
<?php echo do_shortcode("[searchandfilter id="6128"]"); ?> <?php //Set server timezone to central date_default_timezone_set('America/Chicago'); //Today's date $today = date('Y-m-d H:i:s'); ?> <?php //arg to determine if the post is a past event. $args = array( 'post_type' => 'productions', 'posts_per_page' => 10, 'order' => 'DESC', 'meta_query' => array( array( 'key' => 'event_date', 'value' => $today, 'compare' => '<=', ), ) ); ?> <?php // the past events query $query = new WP_Query( $args ); ?> <?php if ( $query->have_posts() ) : ?> <!-- the loop --> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class( 'cf' ); ?> role="article"> <header class="article-header"> <h3 class="h2"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3> </header> <section class="entry-content cf"> <?php if ( has_post_thumbnail() ) : ?> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <?php the_post_thumbnail('bones-thumb-300'); ?> </a> <?php endif; ?> </section> <footer class="article-footer"> </footer> </article> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php else : ?> <article id="post-not-found" class="hentry cf"> <header class="article-header"> <h1><?php _e( 'Oops, Post Not Found!', 'bonestheme' ); ?></h1> </header> <section class="entry-content"> <p><?php _e( 'Uh Oh. Something is missing. Try double checking things.', 'bonestheme' ); ?></p> </section> <footer class="article-footer"> <p><?php _e( 'This is the error message in the custom posty type archive template.', 'bonestheme' ); ?></p> </footer> </article> <?php bones_page_navi(); ?> <?php endif; ?>
Anonymous(Private) January 24, 2018 at 9:35 pm #154746I 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
$args
variable but returning$query_args
so 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 meta
settings 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