• I am trying to override the default WordPress query to query a specific post type at a specific time. This is the code I am using to override it.

    $args = array(
            's' => $searchParam,
            'post_type' => "paper"
        );
        $post_query = new WP_Query($args);

    I printed out the results of that query because I am not getting the expected results and it is skipping one of the meta_keys in wp_postmeta. There are 5 keys (created as custom fields from the admin dashboard: author, pub_info, year, link, and keywords and the query only compares the search parameter to the first 4. Anyone know why this is happening or how to fix it

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter harpcharley

    (@harpcharley)

    The value does show up when I use get_post_meta and the other fields return the expected results, but I don’t think I’ve done anything different for them, except for adding the keywords field a few days later.

    Thread Starter harpcharley

    (@harpcharley)

    I created a field as “author” and then changed it to “keywords” and it is searched correctly, despite not appearing in the query…I am so confused

    Moderator bcworkz

    (@bcworkz)

    Hmmm… WP by default only searches title, excerpt, and content, not meta data. Any added searched data is implemented by your theme or a plugin.

    To override a WP query, it’s best to modify the main query through the “pre_get_posts” action instead of creating your own new query. If nothing else, doing so makes pagination work by default. Managing pagination through a new query is a bit of a pain.

    To resolve your issue, you should ask in the dedicated support forum for whatever module is responsible for meta searching. However, instead of relying on other code to handle what you want, I recommend explicitly adding your own meta_query argument to search queries so you’re sure to get the results you want. Hook the action with a large priority argument to help ensure your version of the query is what gets applied.

    It’s still possible for other code to modify queries after “pre_get_posts” through other filters like “posts_request”. If this is happening for your modified queries, you may need to also remove any filter hooks that are getting in the way.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘WP_Query is skipping a meta_key’ is closed to new replies.