- This topic has 5 replies, 3 voices, and was last updated 2 years, 11 months ago by
Ying.
-
AuthorPosts
-
March 26, 2023 at 4:48 pm #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.
March 26, 2023 at 5:44 pm #2583326Fernando 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/
March 27, 2023 at 9:00 am #2584374Marco
Thank you so much. It worked!
Also, could you please guide me on ensuring that the search results display only posts and not pages?
March 27, 2023 at 10:46 am #2584527Ying
StaffCustomer SupportHi 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/
March 27, 2023 at 11:28 am #2584613Marco
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?
March 27, 2023 at 1:51 pm #2584761Ying
StaffCustomer SupportYes, I happen to know how to achieve this 🙂
1. You are using a headline block to represent the
Search resultstext, so change the text toSearch 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 querytext 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/
-
AuthorPosts
- You must be logged in to reply to this topic.