Forums › Forums › Search & Filter Pro › Custom post type not showing in "post type" search form UI checkboxes
- This topic has 21 replies, 3 voices, and was last updated 6 years, 1 month ago by
Anonymous.
-
Ross Moderator(Private) August 14, 2019 at 10:55 am #218958
Hi again.
So the absolute easiest way to do this, is perform a search, and then look at the URL:
mysite.com/?sf_myoption=myvalue
Then you can link to this URL from your navigation or wherever you are using a link to the search page, with the URL value in there.
Because you’ve removed the first option too, the search form can appear within the site without the worry of an option not being selected before being used.
There is another more tedious way of modifying the query with another filter – though, we’re improving this for v3 (our next major release) so the above option might be simpler for now.
Let me know your thoughts.
Thanks
Ross Moderator(Private) August 14, 2019 at 12:21 pm #218979Ahhh yes fair enough, if it wasn’t the homepage the above should have worked, but in this scenario I can see why that wouldn’t.
Anyway, I’ve had a little play with some code and this is working for me (where your homepage page ID is
2
) – add this in addition to the code before:add_action("init", "wp_init"); function wp_init(){ //make sure this the homepage only, you might need to adapt this (start by removing if this is not working) if(is_page(2)){ //if post types is not set, set it to 'post; if(!isset($_GET['post_types'])){ $_GET['post_types'] = 'post'; } } }
It’s a little hacky but seems to work just fine in my testing.
Thanks
Ross Moderator(Private) August 16, 2019 at 2:05 pm #219135Hi Thomas
So I’ve found a better way to do this (that can be search form specific).
The code in full (so remove everything else we’ve added:
function filter_input_object_radio($input_object, $sfid){ if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio')) { return $input_object; } if(isset($input_object['options'])){ //remove the first option from the array $removed_option = array_shift($input_object['options']); } //now set the default value in the field //make sure we only modify a field belonging to a specific Search Form, by ID if($sfid==15485){ //replace 15485 with your search form ID //make sure the field is not set if(!isset($_GET['post_types'])){ //then set it to "post" $input_object['defaults'] = array("post"); } } return $input_object; } add_filter('sf_input_object_pre', 'filter_input_object_radio', 10, 2); function set_default_post_type_query( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==15485) //replace 15485 with your search form ID { //modify $query_args here before returning it if(!isset($_GET['post_types'])){ $query_args['post_type'] = 'post'; } } return $query_args; } add_filter( 'sf_edit_query_args', 'set_default_post_type_query', 20, 2 );
The only thing you have to replace is the search form ID of
15485
(there are 2 instances of this) – swap this out to match the ID of the search form on the homepage and it should work 🙂Best
-
AuthorPosts