WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Modify admin query so that keyword is searched for in title AND custom field (5 posts)

  1. mobilewebexpert
    Member
    Posted 6 months ago #

    How do I make it so that when an admin uses the 'Search Posts' form on the 'All Posts' admin page, the search word that he types in is used by WP to return posts where the 'post_title' field *OR* a custom field contains the search word.

    I'm using the pre_get_posts action hook to modify the $query variable but WP seems only to be doing an AND search rather than the required OR search. Here is my code:

    function my_pre_get_posts( $query ) {
        if ( is_admin() && $query->is_main_query() && $query->query['post_type'] === 'post' && isset($query->query['s']) ) {
    
            $search_word = $query->query['s'];
            //echo '<!-- keyword: ' . $search_word . ' -->';
    
            /*
             * This only ANDs the post's title value and its my_field value.
             * (I need it to OR the two values.)
             */
    
            //$query->query_vars['meta_key'] = 'my_field';
            //$query->query_vars['meta_value'] = $search_word;
            //return
    
            /*
             * Same problem as above with this technique.
             */
    
            //get the existing meta query
            $meta_query = $query->get('meta_query');
    
            $meta_query_args = array( //Add our meta query to the original meta query
                'relation' => 'OR', // Optional, defaults to "AND"
                array(
                    'key' => 'my_field',
                    'value' => $search_word,
                    'compare' => 'LIKE',
                ),
                /*
                //Including this sub-array makes no difference at all. It seems WP is using it's own code somewhere to search in post_title.
                array(
                    'key' => 'post_title',
                    'value' => $search_word,
                    'compare' => 'LIKE',
                ),
                 */
            );
    
            //Set $meta_query
            if (is_array($meta_query)) {
                //Add to array
                $meta_query[] = $meta_query_args;
            }
            else {
                //Set/replace array
                $meta_query = $meta_query_args;
            }
    
            //set revised query
            $query->set('meta_query', $meta_query);
            //$query->set('meta_key', 'my_field');
    
            //echo '<!--';
            //print_r($query);
            //echo '-->';
    
        }
    }
    add_action( 'pre_get_posts', 'my_pre_get_posts' );

    I don't think the problem is in my code. I think the problem is that WP is ANDing the meta_query with the post_title in its SQL, so how do I make it OR them instead?

    Thanks in advance!

  2. bcworkz
    Member
    Posted 6 months ago #

    You've pretty much correctly assessed the situation. Except for a few special cases, WP makes reasonable guesses whether to AND or OR clauses together, and you don't have much to say about it. And when it sees the 's' query var it automatically adds both title and content columns to the query with an OR.

    You just need to make a callback to another hook that fires a bit after 'pre_get_posts'. This one is the 'posts_where_paged' filter. Your callback is passed the entire WHERE clause. (and the entire query by reference if you'd rather work with that) Your code just needs to figure out which ANDs need to be converted to ORs. Easy to do on sight, but might be tricky to capture all situations with code.

  3. mobilewebexpert
    Member
    Posted 6 months ago #

    Problem solved now.

    To do an OR search, I tried merging two separate WP_Query results as shown here - http://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s - in guidod's answer. That wasn't a great solution though, and resulted in erratic behaviour.

    The correct solution I found was to modify the query using the WP Custom Query as shown in the code (which requires some modifications) here - http://codex.wordpress.org/Custom_Queries .

  4. dotsvilla
    Member
    Posted 3 months ago #

    mobilewebexpert, could you kindly provide more details as to how you solved it at the end?

  5. mobilewebexpert
    Member
    Posted 3 months ago #

    Sorry, I don't seem to have the code on my computer now, but the solution is most likely in this section - http://codex.wordpress.org/Custom_Queries#Implementing_Custom_Queries .

Reply

You must log in to post.

About this Topic