Site logo

Archived topic

Remove column layout from search results

3 replies · Started by George on July 9, 2022

Viewing posts 1–4 of 4

I am only including a custom post type Courses in the navigation and WP search results, like this:

//Limit navigation results to Courses
add_filter( 'generate_navigation_search_output', function() {
    printf(  
        '<form method="get" class="search-form navigation-search" action="%1$s">
            <input type="search" class="search-field" value="%2$s" name="s" title="%3$s" />
            <input type="hidden" name="post_type" value="courses" />
        </form>', 
        esc_url( home_url( '/' ) ), 
        esc_attr( get_search_query() ),   
        esc_attr_x( 'Search', 'label', 'generatepress' ) 
    ); 
} );

function searchfilter($query) {
 
    if ($query->is_search && !is_admin() ) {
        $query->set('post_type','courses');
    }
 
return $query;
}
 
add_filter('pre_get_posts','searchfilter');

It works as expected. Now, I would like to not display search results in columns (default Customizer behavior), like this:

//Remove columns for search results
add_filter( 'option_generate_blog_settings', 'gm_custom_search_results_page_settings' );
function gm_custom_search_results_page_settings( $options ) {
    if ( is_search() ) {
		$options['column_layout'] = false;
    }

    return $options;
}

Results are still being displayed as columns, though.

Hi George,

try this instead:

add_filter( 'generate_blog_columns', function( $columns ) {
    if ( is_search()  ) {
        return false;
    }
    return $columns;
} );

If that doesn't work then your other query is interfering the the is_search condition and we'll need to think of something else

That's it, thanks, David!

Glad to hear that!

This archived topic is closed to new replies.