1

i have an meta_key 'reviewer_for_post' which includes 'reviewer_id' as an array key but how can i get it's value inside meta_query Here what i want to do

'meta_query'     => array(
            array(
                'key'     => 'reviewer_for_post[reviewer_id]',
                'value'   => $user_editor->ID,
                'compare' => '='
            ),
        ),

i want to compare [reviewer_id](which is inside 'reviewer_for_post' key) value with $user_editor->ID value

5
  • So if $user_editor->ID was 5, you are trying to look for posts that have the key reviewer_for_post[5] set, is that what you mean? Then insert the user id into the key, remove value, and use EXISTS as the comparison operator. Commented Jun 23, 2023 at 13:02
  • @CBroe actually, i want to compare 'reviewer_id' value with '$user_editor->ID' and 'reviewer_id' is inside another key called 'reviewer_for_post' which is an array. Commented Jun 23, 2023 at 13:08
  • I can't tell what you mean by that. Can you please show how those meta data records actually look in the database? Commented Jun 23, 2023 at 13:10
  • @CBroe here meta_key = 'reviewer_for_post' meta_value = 'a:4:{s:11:"reviewer_id";s:2:"56";s:13:"reviewer_name";s:12:"Michal Aibin";s:12:}' Commented Jun 23, 2023 at 13:12
  • Okay, so your key name is not reviewer_for_post[reviewer_id], but just reviewer_for_post. The value appears to be an array stored in PHP's serialize format (although the given example seems to be missing something, can't be unserialized.) There is no direct way to search for specific "entries" within this text value - you will have to use the LIKE operator, and search for values that contain %s:11:"reviewer_id";s:2:"56"% In place of the 56 you will have to insert your reviewer ID, and the 2 in s:2 before that needs to be replaced with the number of digits the ID has. Commented Jun 23, 2023 at 13:46

1 Answer 1

0

AFAIK SQL does not support casting as an array or comparing values in the array (see this question). What you can do though, is get as narrow a result as possible with SQL, and then use PHP to narrow down further (untested):

$query = new WP_Query( array(
    'meta_query' => array( array(
        'key'     => 'reviewer_for_post',
        'compare' => 'EXISTS',
    ) ),
);

foreach ( $query->posts as $i => $post ) {
    $reviewer = get_post_meta( $post->ID, 'reviewer_for_post', true );

    if ( is_array( $reviewer ) && ! empty( $reviewer['reviewer_id'] ) && 5 === $reviewer['reviewer_id'] ) {
        continue;
    }

    unset( $query->posts[ $i ] );
}

You can also use a LIKE comparison, but as mentioned in the other question, this is unstable and susceptible to instability.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.