Site logo

Archived topic

Customizing search results page

16 replies · Started by Alberto on January 16, 2018

Viewing posts 1–15 of 17

Hello!
I’m trying to get something like what Otto stated here but in my case I don’t use Elementor, just GP Pro.
I want to display the results as standard posts, i.e., as you are displaying the posts here at https://generatepress.com/support/
The results can be from an event, company data or a news entry (all of these CPT), so I would like to show, along with the word(s) searched, the category or type of post in which it appears.

I'll appreciate any help.

Thanks in advance.

Hi there,

Our support forum is using bbPress, so it's not the regular post loop.

Can you show me an example of the layout you're wanting to achieve?

Hello Tom.
It's really something basic, I think.
You can see an image of what I'm trying to get here: https://prnt.sc/i1o8zx
I want to show only the title of the post and the section it is in, with a link to the post.
No problem with the post in Nieuws or in Standhouders, but the information I enter in Spreker (a CPT) is showed in a sidebar of Programma's page (see here: http://duitslanddag.nl/de-sprekers) so I want to link the last entry (showed in image) to the event page and not to the spreker page, i.e., http://duitslanddag.nl/events/duitslandervaring-jaap-schalken/ and not http://duitslanddag.nl/spreker/jaap-schalken/

What are the files I've got to edit and how can I achieve this?

Thanks in advance!

Alberto.

BTW, using a plugin like Search Everything I get Sprekers or Standhouders in the results because it let me serach in custom fields or custom taxonomies, but I would like to use the search tools provided by WP (are the one you use in GP, isn't it?) How can I do to include custom fields and taxonomies in the results with GP Search tool?

Thanks!

You could get close using CSS like this:

.search-results article {
    float: none !important;
    width: 100%;
}

.search-results .entry-summary,
.search-results .read-more-container {
    display: none;
}

However that won't show the category next to the title. You could turn it on like this:

add_filter( 'generate_show_categories', 'tu_search_results_categories' );
function tu_search_results_categories( $cats ) {
    if ( is_search() ) {
        return true;
    }

    return $cats;
}

Then you'd need some more CSS - let me know if you need help with it :)

Hello Tom!
The CSS code works but it doesn't the filter.
Besides, it only displays the posts in Nieuws, not in Standhouders neither Sprekers/Programma (these are Custom Post Types).

Any thoughts?

Thanks in advance!

I was trying to do the search also with CPT (and Custom Fields!) with something like this, but it didn't works.

function dom_cpt_search( $query ) {
    if ( is_search() && $query->is_main_query() && $query->get( 's' ) ) {
    
        $query->set(        
            'post_type', array('post', 'page', 'product'),
            'meta_query', array(
                array(
                'key' => 'wysiwyg',
                'value' => '%s',
                'compare' => 'LIKE',
                ),
            )
        );        
        return $query;
    }
}
add_action( 'pre_get_posts', 'dom_cpt_search' );

Am I on the right track?

Can you link me to an example search results page? That filter should work.

That function looks right, the only part I'm not sure about is the meta_query parameter. What's the %s value you're trying to compare with?

Hello Tom.
Sorry for taking so long to reply.
Yes, you can take a look at: http://duitslanddag.sjbdixital.net/
If you search "duitse" you can see posts in Nieuws and even in the first result that belongs to a custom tax (category in a CPT), but no results that matches custom fields like Sprekers (see Alberto PM: Xestor Duitse) or Standhouders, another CPT (see Empresa A: Description field).

Your filter is in functions.php file.

Mmmm about the function above... I found that pice of code in https://wordpress.stackexchange.com Yes, I think there's an error there. I think it must be the search term, something like $search_term = $query->query_vars['s']; but I can't get it working. Any ideas?

Thanks in advance!

Been reading over the details on customizing the search results page. I have a need where I would like to remove the Featured Image graphic from my search results. Is there a way to accomplish this in CSS?

John

Or. Another option in the search results that might be handy is to have the featured image shown with text beside the image, as opposed to under the image.

Try this PHP snippet:

add_filter( 'option_generate_blog_settings','lh_remove_search_featured_image' );
function lh_remove_search_featured_image( $options ) {	
	if ( is_search() ) {
		$options[ 'post_image_position' ] = '';
	}	
	return $options;
}

Adding PHP: https://docs.generatepress.com/article/adding-php/

Hi Leo.
Thanks for the note. I gave the snippet a try, using the Snippets plugin. I do not see any impact on the search results page (image is still present).

John

Ahh try this instead:

add_filter( 'option_generate_blog_settings', 'lh_remove_search_featured_image' );
function lh_remove_search_featured_image( $options ) {
    if ( is_search() ) {
        $options['post_image'] = false;
    }
    return $options;
}
This archived topic is closed to new replies.