[Support request] Customizing search results page

Home Forums Support [Support request] Customizing search results page

Home Forums Support Customizing search results page

  • This topic has 16 replies, 4 voices, and was last updated 6 years ago by Leo.
Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #472403
    Alberto

    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.

    #472876
    Tom
    Lead Developer
    Lead Developer

    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?

    #473104
    Alberto

    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.

    #473108
    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!

    #473775
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #474696
    Alberto

    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!

    #474712
    Alberto

    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?

    #475105
    Tom
    Lead Developer
    Lead Developer

    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?

    #477910
    Alberto

    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!

    #478566
    Tom
    Lead Developer
    Lead Developer

    Just to confirm here – you’re wanting the search results page to include posts from your custom post types, correct?

    If so, this should be all that’s needed: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Include_Custom_Post_Types_in_Search_Results

    #535699
    John

    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

    #535702
    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.

    #535791
    Leo
    Staff
    Customer Support

    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/

    #536092
    John

    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

    #536320
    Leo
    Staff
    Customer Support

    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;
    }
Viewing 15 posts - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.