• Resolved kptgreg

    (@kptgreg)


    This does not seem to search custom post types, is there a way I can add that?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Hello,

    This plugin also searches custom post type, but it will not search if the post type excluded from the search when post type registered.

    You can use this code to get the result
    Please add below code in your active theme functions.php file

    function rc_add_cpts_to_search($query) {
    
    	// Check to verify it's search page
    	if( is_search() ) {
    		// Get post types
    		$post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'objects');
    		$searchable_types = array();
    		// Add available post types
    		if( $post_types ) {
    			foreach( $post_types as $type) {
    				$searchable_types[] = $type->name;
    			}
    		}
    		$query->set( 'post_type', $searchable_types );
    	}
    	return $query;
    }
    add_action( 'pre_get_posts', 'rc_add_cpts_to_search' );
Viewing 1 replies (of 1 total)
  • The topic ‘Search custom post types’ is closed to new replies.