pre_get_posts hook that never works
I used the following code both in functions.php of my theme and in my custom plugin in the "after_theme_setup" hook (pre_get_post code only as it is alot to put in one question at once already)
/* The pre_get_posts code: */
add_action('pre_get_posts', 'ipgh_pre_get_posts', 10);
function ipgh_pre_get_posts( $query ) {
if(is_admn() || ! $query->is_search() || ! $query->is_main_query() ) return;
$keyword = $query->get('s');
/* Just a test and doesn't even work either */
$query->set('post_type', 'product_variation');
/* My workaround, atleast I thought it would work: */
$custom_post_types = get_posts([
'post_type' 'product_variation',
'post_status' => 'publish',
'fields' => 'ids',
/* Optional, I tried with and without it as it internally does change the SQL structure */
's' => $keyword,
'meta_query' => [
'relation' => 'OR',
[
'key' => '_sku',
'value' => $keyword,
'compare' => '='
],
[
'key' => 'brand',
'value' => $keyword,
'compare' => 'LIKE'
],
]
]);
/* This runs smoothly and indeed returns what I want. The IDs of the posts
( which are children of another post of type "product" but that shouldn't
matter as they are all posts anyway, plus I've searched for child posts
by their title and that works natively in Wordpress
so it should work either way )
*/
var_dump( $custom_post_types );
/* This is where it just doesn't do anything, if you dump the query
it seems that the query wasn't even changed at all
*/
$query->set( 'post__in', $custom_post_types );
/* Even tried this but ofcourse, doesn't work
Note: I tried with both post__in and post_parent__in
and each of these options seperately. Also tried to get
their parent IDs and then pass them to post_parent__in
but doesn't work.
*/
$query->set( 'post_parent__in', $custom_post_types );
}
QUESTION: I want to be able to search with meta data AND post title like if theres no meta found then just the posts by title. But not matter how I change the query inside the pre_get_posts the frontend shows "no results" no matter what. Even if I change only one simple query_var.
Is there a way to just force the search in such way that I either add SQL myself or edit the template to force my own query or is there some hook where I can just return the plain posts that I need?
pre_get_posts
, but keep in mind that the search may not do what you're expecting/hoping it to do, but it's difficult if not impossible to tell without seeing what you attempted. Use theEdit
link to update your question to include the missing information, in particular the code for the hooks you tried, and the workingget_posts
exampleOR
? AKA if I search forbanana
you not only want it to return CPT's with the meta that matchesbanana
, but title and content too? Specifically it can have one or all of the above? Or do all of them have to match? Are you sure you don't have otherpre_get_posts
filters interfering with this? Ifget_posts
returns what you expected then how is its query args different from the main query? (get_posts
is a wrapper aroundWP_Query
with a few extra quirks )