WordPress.org

Forums

Possible bug when querying multiple Advanced Custom Fields? (1 post)

  1. Igneosaur
    Member
    Posted 1 year ago #

    So I'm just trying to create an OR query on multiple Custom Fields, I have followed the documentation both on the WordPress docs and the ACF docs but the query seems broken, almost like it is attempting an 'AND' rather than an 'OR'.

    Here's the code I've got:

    $args = array(
        'post_type' => 'post',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'description',
                'value' => $searchTerm,
                'compare' => 'LIKE'
            ),
            array(
                'key' => 'title',
                'value' => $searchTerm,
                'compare' => 'LIKE'
        ),
    );

    There are the args I'm putting into WP_Query(); It doesn't work. If I change it to querying a single field (just the description) it works fine:

    $args = array(
        'post_type' => 'post',
        'meta_query' => array(
        array(
            'key' => 'description',
            'value' => $searchTerm,
            'compare' => 'LIKE'
        )
    );

    Since it's an 'OR' and it seems the code is right it should come back with the same results, but it comes back with incorrect results. It is like an 'AND' comparison.

    I'll explain the results:
    Let's say I'm querying the word 'horatio'. There are two records; record 1 has 'horatio' both in the title and the description, record 2 just has that string in the description.

    If I do the query on just the 'title' field I get back record 1 (expected). If I do the query on just the 'description' I get back both records 1 and 2 (expected), but if I do the query on both fields I just get record 1 (I expect to get both, with an OR statement).

    Unfortunately I am running some pretty old versions of WordPress (3.1) and Advanced Custom Fields (2.1.4), I cannot do anything about this at the moment.

    I have spoken to the support team at ACF and they don't see anything wrong with my query.

    Is this a bug, or am I doing something terribly wrong?

Topic Closed

This topic has been closed to new replies.

About this Topic