Support

Account

Home Forums General Issues “meta_query” requires manual update in the admin

Solved

“meta_query” requires manual update in the admin

  • I’m facing an issue with custom fields for users, even though I think that’d happen for posts too.

    I have a group “custom_fields” and within this group, there’s a multi-select whose key is “user_lang_spoken”.

    This multi-select is pulling a custom taxonomy and it will return a list of term IDs.

    Ok, so far so good and easy to understand. This is my custom loop to pull users:

    
    $speaks = 5; // 5 = Spanish (That's the term name)
    
    $args = array(
        'fields' => 'ids',
    );
    
    if ($speaks) {
        $args['meta_query'][] = array(
            'key' => 'custom_fields_user_lang_spoken',
            'value' => $speaks,
            'compare' => 'LIKE',
        );
    }
    
    $members = get_users($args);
    

    #1 – You’ll see that I’m using ‘value’ => $speaks, and this is pulling all users who speak language 5 or language 55 (which is not ideal). This could actually work if it wasn’t numbers because we wouldn’t have a partial match, but I need them as IDs.

    #2 – However, I found another topic where you guys say it returns serialized data and you recommend wrapping the value with quotes: ‘value’ => ‘”‘.$speaks.'”‘, and this is true, I’ll retrieve only users who speak language 5, but for that I need to MANUALLY update the user in the admin (without changing anything). Otherwise, I wont be able to see the user in this case.

    I think this is an actual issue, it won’t happen often because people may be returning strings instead of IDs and by doing that, the chances of a partial match is rare. Also, if they are using IDs, they might have different IDs such as 2918, 9812, 5839… which again, would be rare.

    However, that partial match will happen with lower numbers: 1 -> 12, 3 -> 32…

    I don’t know what else to do, I tried some work around to force a user update on the frontend but it doesn’t seem to have the same impact as manually clicking on “Update user”.

    Thanks!

  • Yes, you need to wrap the value in quotes

    
    'value' => '”'.$speaks.'”',
    

    your meta key should be just the field name, do not include the field group name

    
    'key' => 'user_lang_spoken',
    
  • Still nothing, same issue.

    Not including the group name always returns empty. In addition to that, all tutorials I’ve read asked to add the group name, so much so that it works, but with the problem that I mentioned:

    $speaks = 9;

    (no quotes) ‘value’ => $speaks = returns a partial match too (9, 19, 29…)

    (with quotes) ‘value’ => ‘”‘.$speaks.'”‘ = returns a full match as it should, but only if I, at some point, manually clicked on “Update user” in the backend.

  • Are you talking about a “Field Group” or a “Group Field” that is named “custom_fields”?

    A field will not have a value unless you update the user. Are you expecting to search on the “default value” of the field without saving the value? This cannot be done. A default value does not automatically add values to usermeta, you must update each user before there is a value to search on in the DB.

  • Group field, the field has a value that was updated on the frontend with update_field(), I’m able to pull the value and display it normally.

    However, the problem is only when filtering with “meta_query”, I’m not able to pull users with that value unless I manually update the user in the backend (without changing anything).

    Just by doing that it will work and display the user in the loop, otherwise it doesn’t work, but I can’t manually update all users every time.

    So again, everything works, I can update the value and display it, etc. But when using “meta_query” to filter users it doesn’t work until I manually update that user.

  • now I am beginning to understand.

    When using update_field() for a field that does not have a value yet you must use the field key and not the field name. See the section on using field keys in the documentation for an explanation. https://www.advancedcustomfields.com/resources/update_field/

  • It was not it, I just tried something and it worked.

    I was updating the field with term IDs as integers: 1, 2, 3…
    Then I tried updating the field using strings instead: “1”, “2”, “3”…

    Now it’s working, that was the difference when updating on the frontend/backend.

    Anyway, just another day in the life of a developer.

    Thanks for helping me clarify!

  • another thing that I just thought of, you are using a group field with sub fields… You can’t update the sub field directly using update_field(). You either have to update the group field with an array of sub fields (field key => value pairs), or you need to use update_sub_field(), https://www.advancedcustomfields.com/resources/update_sub_field/

    Yes, a taxonomy field holds and array of term ID as string values.

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.