• Resolved SJW

    (@whitsey)


    I have created a custom post type service-provider with custom taxonomies region and service-category – By itself, this search works as expected BUT I want to also search on an ACF field.

    I have an ACF field on the service-provider post type called organisation

    I have added a meta_query with relation = OR to search with the keyword and find a match either in the post content OR the ACF field.

    I have a post with Title = “Accommodation Service” and ACF Field “organisation” = “Dawn Inc”

    I want the search to show results where the keyword matches either the post title or the acf field.

    Here is my query:

    <?php
    // Handle form submission
    $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( $_GET['keyword'] ) : '';
    $region = isset( $_GET['region'] ) ?  array_map('sanitize_text_field', $_GET['region']) : array();
    $service_category = isset( $_GET['service-category'] ) ? array_map('sanitize_text_field', $_GET['service-category']) : array();
    
    if ( !empty($keyword) || !empty($service_category) ) :
    
        $args = array(
            'post_type'      => 'service-provider',
            'posts_per_page' => -1,
            's'              => $keyword,
            'meta_query'     => array(
                'relation'    => 'OR',
                array(
                    'key'     => 'organisation', 
                    'value'   => $keyword,
                    'compare' => 'LIKE',
                )
            ),
        );
    
        if ( ! empty( $region ) ) 
        {
            $args['tax_query'][] = array(
                    'taxonomy' => 'region',
                    'field'    => 'slug',
                    'terms'    => $region,
            );
        }
    
        if ( ! empty( $service_category ) ) 
        {
            $args['tax_query'][] = array(
                    'taxonomy' => 'service-category',
                    'field'    => 'slug',
                    'terms'    => $service_category,
            );
        }
    
        $query = new WP_Query( $args ); ?>

    When I search for “dawn” I get no results. So I dumped the $query to see the SQL and this is what I got:

    SELECT   wp_posts.*
    FROM wp_posts  
    INNER JOIN wp_postmeta 
        ON ( wp_posts.ID = wp_postmeta.post_id )
    WHERE 1=1  
        AND (((wp_posts.post_title LIKE '%dawn%') OR (wp_posts.post_excerpt LIKE '%dawn%') OR (wp_posts.post_content LIKE '%dawn%')))  
        AND ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) ) 
        AND ((wp_posts.post_type = 'service-provider' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' 
    OR wp_posts.post_status = 'private')))
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_title LIKE '%dawn%' DESC, wp_posts.post_date DESC

    I see that the meta_query is performing an AND relation.

    When I run this query straight in MySQL I get no results BUT, if I change the AND to an OR on this line:

    FROM:

    AND ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) )

    TO:

    OR ( ( wp_postmeta.meta_key = 'organisation' AND wp_postmeta.meta_value LIKE '%dawn%' ) )

    I get the result I am expecting to see…

    So, if I have already set the meta_query to OR and it is giving me this result – how to I fix this query to give me the results I expect?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I hope you’ll forgive me for not carefully reading your entire post. From what I did read I’m confident I know what’s wrong and can advise on what to do. I do appreciate your thoroughness and the complete details of your issue all the same.

    The meta_query ‘OR’ directive only applies to logic between multiple meta data criteria, it does not apply to other WP_Query criteria like tax_query, post_type, etc. WP_Query automatically applies AND logic to all other criteria. Except for search logic, that’s a little different, but it’s only within the various searched fields. Since you only have one meta_query criteria, the ‘OR’ directive there has no effect.

    Using WP_Query parameters, there’s no way to specify OR logic between the various parameters, logic by default is always AND. The only way to get OR logic is to use the “posts_request” filter to alter the actual SQL that WP_Query had come up with. Search the SQL for the undesired ANDs and replace them with ORs. Just as you’ve done by doing a straight SQL query. You’ve kind of answered your own question without realizing it ??

    The only alternative is to compose your own SQL query from scratch and execute it with global $wpdb methods.

    Thread Starter SJW

    (@whitsey)

    Makes complete sense. Thanks.

    Found a plugin that resolves it out of the box: ACF Better Search

    • This reply was modified 10 months, 3 weeks ago by SJW.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query not using relation key as expected and not producing any results’ is closed to new replies.