Forums Forums Search & Filter Pro Custom post type not showing in "post type" search form UI checkboxes

Viewing 10 posts - 11 through 20 (of 21 total)
  • Ross Moderator
    #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

    Anonymous
    #218966
    This reply has been marked as private.
    Ross Moderator
    #218979

    Ahhh 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

    Anonymous
    #218987

    Thanks, is this to be added to the previous filter function, or as an entirely different function as you have it in your message?

    Anonymous
    #218989

    I’m asking because copying and pasting it as such in functions.php doesn’t do anything 🙂

    Anonymous
    #218991

    Ok well, it works if I remove the if is_page part.

    I understand this won’t affect search forms where the filter has not been removed so that should work fine.

    Thanks for the great support, as usual!

    Anonymous
    #218993
    This reply has been marked as private.
    Anonymous
    #219115

    Hi Ross,

    Have you had a chance to consider what could be the issue here? As explained, your code breaks other search forms by causing them to query posts instead of the cpt defined in admin.

    Thanks,

    Ross Moderator
    #219133
    This reply has been marked as private.
    Ross Moderator
    #219135

    Hi 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

Viewing 10 posts - 11 through 20 (of 21 total)