• Resolved Cezar Ayran

    (@ayrancd)


    In the back-end of a custom post type (post_type=inventory) I’d like the search to also search for a custom field… not it searches the title which is fine, I found the following code but when I add it, it only searches by the stock # it doesn’t search the title anymore… how to improve it? I want to keep the default search and also add the extra custom field in the query.

    function custom_search_query( $query ) {
        $searchterm = $query->query_vars['s'];
        $query->query_vars['s'] = "";
    
        if ($searchterm != "") {
            $meta_query = array('relation' => 'OR');
            array_push($meta_query, array(
                    'key' => 'stock',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ));
    
    
            $query->set("meta_query", $meta_query);
        };
    }
    add_filter( "pre_get_posts", "custom_search_query");
Viewing 10 replies - 1 through 10 (of 10 total)
  • You can use Relevanssi Plugin for better search with ACF.

    Thread Starter Cezar Ayran

    (@ayrancd)

    @lahorimela I’d like not to use a plugin

    To improve this code and keep the default search as well as add the extra custom field in the query, you can modify the meta_query to add a condition for the default search fields.

    Here’s an updated version of the code:

    function custom_search_query( $query ) {
        if ( !is_admin() && $query->is_main_query() && $query->is_search() && $query->get('post_type') == 'inventory' ) {
            $searchterm = $query->query_vars['s'];
            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => 'stock',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
                array(
                    'key' => '_meta_or_title',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
            );
            $query->set("meta_query", $meta_query);
            $query->set("s", '');
        }
    }
    add_filter( "pre_get_posts", "custom_search_query");

    This code checks if the query is a main query and is a search query and the post type is inventory. It then sets the meta_query with both the custom field stock and a placeholder _meta_or_title to represent the default search fields. The s variable is also cleared to ensure only the custom fields are searched.

    Thread Starter Cezar Ayran

    (@ayrancd)

    @faisalahammad thank you for your help! It works with the custom field but it doesn’t work with the title still.

    This is from the back-end so I made this change to say that it should only be applied if I’m on this page /wp-admin/edit.php?post_type=inventory

    I also changed the placeholder with title but it is not working

    function custom_search_query( $query ) {
        if (is_admin() && $_GET["post_type"] == "inventory") {
            $searchterm = $query->query_vars['s'];
            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => 'stock',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
                array(
                    'key' => 'title',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
            );
            $query->set("meta_query", $meta_query);
            $query->set("s", '');
        }
    }
    add_filter( "pre_get_posts", "custom_search_query");
    • This reply was modified 1 year, 9 months ago by Cezar Ayran.

    To search the title along with the custom field, you need to modify the code as follows:

    function custom_search_query( $query ) {
        if (is_admin() && $_GET["post_type"] == "inventory") {
            $searchterm = $query->query_vars['s'];
            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => 'stock',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
            );
            $query->set("meta_query", $meta_query);
            $query->set("s", $searchterm);
        }
    }
    
    add_filter( "pre_get_posts", "custom_search_query");

    This code sets the s variable back to $searchterm so that the default search fields, including the post title, are also searched.

    Thread Starter Cezar Ayran

    (@ayrancd)

    @faisalahammad I tried this code but still not working ?? now it doesn’t work with the stock field and title

    function custom_search_query( $query ) {
        if (is_admin() && $_GET["post_type"] == "inventory" && $_GET["s"] != "") {
            $searchterm = $query->query_vars['s'];
            $meta_query = array(
                'relation' => 'OR',
                array(
                    'key' => 'stock',
                    'value' => $searchterm,
                    'compare' => 'LIKE'
                ),
            );
            $query->set("meta_query", $meta_query);
            $query->set("s", $searchterm);
        }
    }
    
    add_filter( "pre_get_posts", "custom_search_query");
    Thread Starter Cezar Ayran

    (@ayrancd)

    @faisalahammad If I use

    $query->set("s", '');

    Stock field works and title doesn’t work, if I use

    $query->set("s", $searchterm);

    None of them work (stock and title).

    Moderator bcworkz

    (@bcworkz)

    Hi Cezar,

    I admit I didn’t thoroughly read this topic in its entirety, but I think I know what the problem is. The default ‘s’ search query uses OR logic among title, excerpt and content fields. That’s good. Your meta query’s OR relation only applies to multiple meta query criteria. The meta query clause is still ANDed with the search clause so both the search clause and meta query clause must both be true for a result to be returned. I’m pretty sure you want OR logic between the two, not AND.

    Sadly, there is no query var we can use to get that kind of OR logic. The only way to get it is to use some sort of search and replace operation on the actual SQL passed in the “posts_request” filter. Find the offending AND and replace it with OR. You don’t want to do this with any query. Limit its application by adding the filter in your pre_get_posts callback.

    Thread Starter Cezar Ayran

    (@ayrancd)

    @bcworkz this is bad :/ don’t even know what to say lol

    Thread Starter Cezar Ayran

    (@ayrancd)

    Well, since we can’t find a solution I’ll mark this as “done”.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Search by custom field’ is closed to new replies.