Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Results template Query Loop Issues in 2024 #58949

Closed
rawdolphe opened this issue Feb 12, 2024 · 10 comments
Closed

Search Results template Query Loop Issues in 2024 #58949

rawdolphe opened this issue Feb 12, 2024 · 10 comments
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Type] Question Questions about the design or development of the editor.

Comments

@rawdolphe
Copy link

Description

The 'Search Results' template seems broken; when setting 'Query Loop' to Layout > Settings > POST TYPE > Post.
The search results pulls ALL posts / Always, regardless of the keyword used. Same when set to 'Page'.

Step-by-step reproduction instructions

  1. Load 'Search Results' template.
  2. Go to 'Query Loop' in 'List View' on the left hand side.
  3. Got to Block 'Settings' and click on 'Settings' (Inherit query from template) and make it > POST TYPE > Post
  4. Go to the front end and do a search (assuming you have a search Block icon OR just hit /?s=any_keyword...)
  5. The Search Query will output ALL posts, regardless of the keyword used, so it doesn't filter anything, and it always output all posts.
  6. Repeat for 'Page' = same issue.
  7. Re-set it to default 'Inherit query from template' and it will pull posts and pages, as it should.
  8. So the problem is when set to Post or page. To me it's broken, as these settings don't do anything at all.
  9. I tried a plugin called Ajax Search Lite, I set to show Post only, and it worked fine, Even with the native Gutenberg Search Block.

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 6.4.3 Default settings.
Problem with ALL browsers.
Problem with ALL OS.

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@rawdolphe rawdolphe added the [Type] Bug An existing feature does not function as intended label Feb 12, 2024
@jordesign jordesign added [Block] Query Loop Affects the Query Loop Block [Feature] Template Editing Mode Related to the template editor available in the Block Editor labels Feb 12, 2024
@jordesign
Copy link
Contributor

Hi @rawdolphe - just thinking on this one a little further - my expectation is that the Query Loop in a search template will need to use the 'Inherit main query' option to actually show the search results.

For any Query Loop block - selecting options manually will always set up a new query, rather than doing something like filtering the main query.

@jordesign jordesign added the [Status] Needs More Info Follow-up required in order to be actionable. label Feb 12, 2024
@rawdolphe
Copy link
Author

Hi @jordesign, I am trying to make sense of this, but still can't work it out:

  1. How do we output Posts only in the Search and
  2. Why are the Post / page setting options there and if they don't do anything useful in the Search template.

So far, all I can see is that the Search will only output everything; Posts AND Pages. Doesn't make sense.

@carolinan
Copy link
Contributor

Can I close your other ticket? It is not beneficial to keep the discussion in two places.

@t-hamano
Copy link
Contributor

t-hamano commented Feb 13, 2024

How do we output Posts only in the Search and

The Search block internally has a query as an attribute. You should be able to achieve this by defining post_type via a filter or registerBlockVariation.

registerBlockVariation( 'core/search', {
   name: 'test/custom-search',
   title: 'Test Search Block',
   category: 'test',
   attributes: {
      query: {
         post_type: 'page'
      }
   }
} );

See: #40585

Why are the Post / page setting options there and if they don't do anything useful in the Search template.

These setting options appear when "Inherit query from template" is disabled. That is, it is used to separate it from template queries. So even if you enter any keyword in the Search block, the query will be ignored. An example of disabling this option is to display a pre-filtered list of posts as part of the page's content.

@carolinan
Copy link
Contributor

Yes you can use the search block to pass different query parameters. In other words pass both the post type and the search term that the visitor enters into the field.

This will only work when the search is initiated from that specific search block.
So if someone types /?s=whatever into the address bar it will still show both pages and posts.

These two search blocks will show different results:

<!-- wp:search {"buttonText":"Search","fontSize":"medium"} /-->

<!-- wp:search {"buttonText":"Search posts","query":{"post_type":"post"}} /-->

The plugin probably uses a PHP filter, which would change the search site wide.

@rawdolphe
Copy link
Author

rawdolphe commented Feb 13, 2024

Can I close your other ticket? It is not beneficial to keep the discussion in two places.

Sure, I only posted here on your recommendation in my original post. Thank you.

PS: I am not fully clued on on the terminology and/or the ins and outs of posting WordPress related issues. Sorry.

For example, I am testing the Search Block (another 'search' related issue funny enough...) in the header and there are several bugs, where should I talk about it? can I explain it here as it's Search related or not?

@rawdolphe
Copy link
Author

Thank you all for trying, I don't know what to do with it, sorry :)

So I chose to pay a visit to my friend chatGPT and he offered the following code, I tested it and it's working now.

My requirement is very simple in the Search 'behaviour'——Hopefully WordPress will update or fix (or improve...) the Search feature in the next update. For now, I am a happy camper.

———
Hi dear friend, I use WordPress Gutenberg and need a php code so that when I hit search with a keyword it will only output posts in the search result. At present the search is hard coded and pulls posts and pages unfortunately. Thank you

Certainly! You can customize the search query in WordPress to only retrieve posts instead of both posts and pages. You can achieve this by adding a filter to modify the search query using the pre_get_posts hook. Here's a PHP code snippet you can add to your theme's functions.php file or in a custom plugin:

// Modify WordPress search to only include posts
function exclude_pages_from_search($query) {
    if ($query->is_search && !is_admin()) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts', 'exclude_pages_from_search');

This code hooks into the pre_get_posts action, which allows you to modify the query parameters before WordPress retrieves posts. It checks if the query is a search query ($query->is_search) and if it's not in the admin area (!is_admin()). If both conditions are met, it sets the post_type parameter of the query to 'post', ensuring that only posts are returned in the search results.

After adding this code snippet, your search results should only include posts, excluding pages. Make sure to test it to ensure it works as expected.
———

I then asked: "Is there any potential issue with this code that I should be aware of?" to which he replied but will save the answer unless you are interested / curious...

If you have a better option please let me know, I would be interested!

@t-hamano
Copy link
Contributor

For example, I am testing the Search Block (another 'search' related issue funny enough...) in the header and there are several bugs, where should I talk about it? can I explain it here as it's Search related or not?

I think it's best to report other issues as new issues.

I then asked: "Is there any potential issue with this code that I should be aware of?" to which he replied but will save the answer unless you are interested / curious...

That code should be fine. A similar code example is also introduced on the pre_get_posts hook.

I think that the search result template and query loop block are currently working as per specifications, but is it safe to close this issue?

@rawdolphe
Copy link
Author

Sure, please free to close it. Thank for the link, will have a look.

Otherwise, where do I report the issues with the Search Block?

@t-hamano
Copy link
Contributor

@rawdolphe Thank you, I would like to close this issue.

Otherwise, where do I report the issues with the Search Block?

Simply create a new issue.

@t-hamano t-hamano closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2024
@t-hamano t-hamano added [Type] Question Questions about the design or development of the editor. and removed [Type] Bug An existing feature does not function as intended [Status] Needs More Info Follow-up required in order to be actionable. labels Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Query Loop Affects the Query Loop Block [Feature] Template Editing Mode Related to the template editor available in the Block Editor [Type] Question Questions about the design or development of the editor.
Projects
None yet
Development

No branches or pull requests

4 participants