• Resolved lmobile

    (@lmobile)


    Dear support is it possible to register CPTs to frontend search? We used this functions.php code, but it causes backend errors with backend search of wordpress.

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( $query->is_search() ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'mitarbeiter', 'referenzen', 'infothekbeitrag', 'partner', 'referenzbericht', 'jobboerse'     );
    
    		$query->set(
    			'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );

    Is there a way to avoid backend search problems while registering to frontend search?

    Thanks in advance Marc

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Have you made sure the post type is 1) public, 2) publicly queryable, and 3) not excluded from search? Logically there’s no reason you should be having to include it manually.

    That said, regarding your code above, I’d recommend checking if is_admin() and return early in those cases. Will prevent affecting your backend searches.

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( is_admin() ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'mitarbeiter', 'referenzen', 'infothekbeitrag', 'partner', 'referenzbericht', 'jobboerse'     );
    
    		$query->set(
    			'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );
    
    Thread Starter lmobile

    (@lmobile)

    Dear Michael

    checked your remarks:

    1. CPTs are public –> active
    2. CPTs are publicly queryable –> active
    3. CPTs exlude from search –> deactivated

    Tried your code –>

    Without code nothing works, no search results. With code no search result in ajax search window but results on search page.

    Your code workd also without backend problems. Is there a way to include search results to ajax search? Old code code showed results also for ajax but with with the problem of backend search errors….

    if ( is_admin() ) {
    		return;
    	}

    this code line prevent the mistakes but in backend but also the showing of search results in the ajax search.

    Thanks for your help Marc

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Ah, AJAX search would be negated by the is_admin() since WP’s AJAX has to load a certain amount of the WP Admin.

    You could use if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { ... } to amend the conditions or possibly nest some if statements.

    Thread Starter lmobile

    (@lmobile)

    Dear Michael

    you mean this way:

    function my_cptui_add_post_type_to_search( $query ) {
    	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
    		return;
    	}
    
    	if ( $query->is_search() ) {
    		// Replace these slugs with the post types you want to include.
    		$cptui_post_types = array( 'mitarbeiter', 'referenzen', 'infothekbeitrag', 'partner', 'referenzbericht', 'jobboerse'     );
    
    		$query->set(
    			'post_type',
    			array_merge(
    				array( 'post' ),
    				$cptui_post_types
    			)
    		);
    	}
    }
    add_filter( 'pre_get_posts', 'my_cptui_add_post_type_to_search' );

    Best Regards Marc

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    I can’t say for sure if that’s the correct method to go about it because it’s a bit of a unique case and I am not set up to test for sure.

    You’d want to return early, for sure, if doing a search from the admin, and you may want both standard and AJAX search if from the frontend. The version you have above would have it work for only non-ajax cases. Maybe check if not doing ajax, and then a second if statement that check if is_admin. That would make AJAX work fine, and then if not, also checks if frontend or not.

    if ( not ajax ) { if ( is_admin() ) { return early } }

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Register CPT UI to frontend search’ is closed to new replies.