0

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?

6
  • can you include the code you've tried in your question? It's not clear how you're displaying your results, what parameters you've attempted, or what the format of the meta values is. In particular I'd be curious to see your 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 the Edit link to update your question to include the missing information, in particular the code for the hooks you tried, and the working get_posts example
    – Tom J Nowell
    Sep 7 at 13:27
  • I do not use anything to display the result, the display results is just traditional wordpress post loops in the theme. I've used other themes and also Wordpress own themes to test it but all the same results and no errors. I try to keep it as close to Wordpress core as possible for extensiveness and stability of the code.
    – Leander
    Sep 7 at 13:57
  • 1
    Have you considered just using a good search plugin like Relevanssi or SearchWP? It might save you a lot of time and hassle, and they work wonderfully out of the box. SearchWP is really excellent.
    – dkeeling
    Sep 7 at 14:17
  • 1
    Yes, I have considered looking for an existing option such as a plugin but they all cost an enormous amount of money for something that could have been soo simple with just a few lines of code. Plus I cannot affort a plugin for the amount of money they offer just to add 2 more custom fields to the search functionality and then end up not using the rest of it's functionality, so I would consider using a plugin but only as my very very last option.
    – Leander
    Sep 7 at 14:28
  • is this one of those situations where you want an OR? AKA if I search for banana you not only want it to return CPT's with the meta that matches banana, 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 other pre_get_posts filters interfering with this? If get_posts returns what you expected then how is its query args different from the main query? ( get_posts is a wrapper around WP_Query with a few extra quirks )
    – Tom J Nowell
    Sep 7 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.