Site logo

[Support request] Generateblocks search page gives duplicated results

Home Forums Support [Support request] Generateblocks search page gives duplicated results

Home Forums Support Generateblocks search page gives duplicated results

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2583290
    Marco

    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.

    #2583326
    Fernando
    Customer Support

    Hi Marco,

    A Block Element – Content Template isn’t meant to have a Query Loop inside it. Having one will result in a loop within a loop of posts, thus, duplication. See here to learn how to use one: https://docs.generatepress.com/article/block-element-content-template/

    An alternative if you want to use a Query Loop Block is to use a Block Element – Loop Template instead. See: https://docs.generatepress.com/article/block-element-loop-template/

    #2584374
    Marco

    Thank you so much. It worked!

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

    #2584527
    Ying
    Staff
    Customer Support

    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/

    #2584613
    Marco

    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?

    #2584761
    Ying
    Staff
    Customer Support

    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/

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.