Site logo

Archived topic

Generateblocks search page gives duplicated results

5 replies · Started by Marco on March 26, 2023

Viewing posts 1–6 of 6

Hello,

I have designed a generatepress element, specifically a Block - Content template, which is set to only display in the Search results section. This element is configured with a simple QUERY LOOP that "inherits the query from the template".

Although I have carefully reviewed the settings and confirmed that the query loop is not duplicated, I have noticed that every time I search for a string that appears in multiple posts, the query loop is unexpectedly duplicated multiple times. As a result, the search page displays an excessively long list of search results with duplicate content.

Can you please advise me on how to resolve this issue? I have provided the relevant link in the private information section.

Thank you.

Thank you so much. It worked!

Also, could you please guide me on ensuring that the search results display only posts and not pages?

Hi Marco,

Try adding this PHP code:

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

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

Thank you for your suggestion!

It would be really helpful if the search query could be displayed in the title of the search result page.

For example, if a user searches for "jeans", the title of the search result page should read "SEARCH RESULTS for 'jeans'".

Do you happen to know how to implement this feature?

Yes, I happen to know how to achieve this :)

1. You are using a headline block to represent the Search results text, so change the text to Search results for "search query".

2. Add an additional CSS class to the headline block, eg.search-query.
https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

3. Add this PHP code which replaces the static search query text with the actual search query.

//replace search query text with actual search_query

add_filter( 'render_block', function( $block_content, $block ) {
    if (  ! empty( $block['attrs']['className'] ) && 'search-query' ===  $block['attrs']['className'] && is_search() )  {
       
		$search_query = get_search_query();
		
		$block_content = str_replace('search query', $search_query, $block_content);
    }

    return $block_content;
}, 10, 2 );

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

This archived topic is closed to new replies.