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

Query Loop block does not display search results in order of relevance #39285

Closed
Tracked by #41405
JakePT opened this issue Mar 8, 2022 · 3 comments
Closed
Tracked by #41405

Query Loop block does not display search results in order of relevance #39285

JakePT opened this issue Mar 8, 2022 · 3 comments
Labels
[Block] Query Loop Affects the Query Loop Block Needs Technical Feedback Needs testing from a developer perspective.

Comments

@JakePT
Copy link

JakePT commented Mar 8, 2022

Description

Normally WordPress search results are sorted by relevance, using relevance for orderby in the query. The Query Loop block however enforces date as the orderby argument by default when "Inherit query from template" is selected. This means that search results are only displayed in date order if the results are displayed using the Query Loop block, as they are in Twenty Twenty-Two.

Step-by-step reproduction instructions

  1. Create a post with a particular word in the title.
  2. Create a second newer post with the same word in the content.
  3. Using Twenty Twenty Twenty-One, or any non-block theme, search for that word. The older post with the word in the title should appear at the top, since it's more relevant due to having the word in the title.
  4. Using Twenty Twenty-Two, or any theme that uses the Query Loop block for search results, and search for the same word. The newer post will appear at the top, even though it's less relevant. This is because it's the most recent post.

A workaround is to use the pre_get_posts hook to correct the order (props @felipeelia):

add_action(
	'pre_get_posts',
	function( $query ) {
		if ( $query->is_main_query() && $query->is_search() ) {
			$query->set( 'orderby', 'relevance' );
			$query->set( 'order', 'DESC' );
		}
	}
);

Screenshots, screen recording, code snippet

No response

Environment info

WordPress 5.9.1
Twenty Twenty-Two 1.1
Gutenberg is not installed.

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

@peterwilsoncc
Copy link
Contributor

When the main query is inherited, I think build_query_vars_from_query_block() needs to do an early return without modifying the default query.

if ( ! empty( $block->context['query']['inherit'] ) ) {
	return array();
}

For when users have set the query parameters, I think an additional option, default, will be needed in the ordering dropdown.

If the keyword/search term is set then the default value with be to order by relevance, if the keyword is not set the current date order will be preserved.

@priethor
Copy link
Contributor

Related to #24934.

@ntsekouras
Copy link
Contributor

This has been resolved by this PR: #40656, where we use the global wp_query when we inherit.

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 Needs Technical Feedback Needs testing from a developer perspective.
Projects
None yet
Development

No branches or pull requests

5 participants