Forums Forums Search & Filter Pro Only return results which match specific post meta data

Viewing 10 posts - 1 through 10 (of 10 total)
  • Anonymous
    #135726

    Hi

    Is it possible to set the following criteria in the Post Meta Tab…

    –POST META TAB–
    “Only return results which match specific post meta data”
    | event_date_time | Timestamp | >= | Current Time Minus 2 Hours |

    Trevor
    #135753

    Not in the Post Meta tab, you you should be able to make something that would work with your own code using this filter:

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    If you search for sf_edit_query_args in the forum, you will also find some posts showing how other users have used the filter, I think.

    Anonymous
    #136903

    Thanks for that, I’ve got it working now but the count on the filters are wrong. The list of filters is now including all posts rather than the ones that match the new query?

    This wasn’t a problem when I used the Post Meta conditions through the plugin UI

    This is the function I have up to now…

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args = array(
    			"meta_query" => array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    Page is at http://www.our-pub.co.uk/wp

    As you can see, the England games under the Football filters have already been played so the England filter should be hidden as it’s empty. ticking the England box and submitting the search returns no results.

    Thanks in advance.

    Trevor
    #136907

    Ah, yes. That would happen, as the count is done after the code you have added. I am not sure you could fix that.

    Anonymous
    #136909

    Just noticed it’s affected the pagination count too. Current page number is missing.

    Anonymous
    #136911

    I can live with the pagination error but the filter count and filters showing when there are no posts is a pretty big problem. There must be a way around it?

    Anonymous
    #136913

    As I said, if I use a Post Meta condition in the UI instead of a function, the filter counts are all correct so is there no way of changing how the Post Meta results are returned?

    Trevor
    #136916
    This reply has been marked as private.
    Anonymous
    #136918

    Thanks.

    Ross Moderator
    #138398

    Hi there

    Just taking a look at this.

    The reason we implemented the edit_query_args filter is exactly for this reason, the arguments get passed through to our count functions too, so these should be updated, and its why this filter should be used over the WP pre_get_posts

    So, this is odd.

    2 things I would suggest:

    1) We are only trying to edit the $query_args (more specifically the meta query), not replace all of them (by using = on the variable you replace anything in the array already), perhaps this is causing an issue. I would change your code above to:

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args["meta_query"] = array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    I would also make sure, that any Post Meta settings, defined in the settings form, are removed / disabled, as well as the order results option in posts tab in your search form (this is all just to be safe and ensure no conflicts).

    2) You could have a pre_get_posts somewhere else in your setup, causing this unusual behaviour, but if using the post meta settings in the form works fine (with the counts) then this may not be the issue. Lets see about option 1 first.

    Let me know how you get on.

    Thanks

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