Forums Forums Search & Filter Pro Search and Filter Pro not working with custom query

Tagged: 

Viewing 9 posts - 1 through 9 (of 9 total)
  • Trevor
    #154725

    You can’t have double quotes inside double quotes. You have this:

    do_shortcode("[searchandfilter id="6128"]")

    Try this:

    do_shortcode('[searchandfilter id="6128"]')

    anywhere you have done this.

    Anonymous
    #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
    #154777

    I 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
    #154903

    I 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.

    Trevor
    #154916
    This reply has been marked as private.
    Anonymous
    #155080

    Ok, 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
    #155099

    Hi 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
    #155101

    BTW, 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.

    Anonymous
    #155191

    The modified query args you suggested worked. Thanks for your help!

Viewing 9 posts - 1 through 9 (of 9 total)