• I need query help!

    I am trying to set up a meta_query (for get_comments) for keys that aren’t yet set.

    I have some code that allows moderators to add a custom meta key to individual comments. Click the checkbox and the meta key is set to shadow, unclick it the meta key is set to an empty string.

    This means that there are three possible states for a comment to have. It can either have the ‘shadow’ meta key, it can have an empty string and the meta key, or the meta key could not be set.

    What I would like to do is query all comments that either don’t have this meta key set or have the meta key that is NOT shadow. It is fairly easy to select all comments that have the meta key not equal shadow but I haven’t found a away to also select comments that don’t have the meta key set.

    This is the code I am using, but it doesn’t work. It selects every comment that has the meta key set, regardless of what it is set to.

    $comments = get_comments( array(
                'meta_query' => array(
                    'relation' => 'OR',
                    array( // Select comments that don't have the 'shadow' p3_comment_status meta
                        'key' => 'p3_comment_status',
                        'value' => 'shadow',
                        'compare' => '!='
                    ),
                    array( // Select comments that don't have the p3_comment_status set
                        'key' => 'p3_comment_status',
                        'compare' => 'NOT EXISTS'
                    )
                )
                )
        );

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    Any help would be appricieated.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • This seems to be related to bug #23268.
    Have you tried setting any value in the second array (I know it doesn’t make sense, but apparently it may fix the problem).

    array(
        'key' => 'p3_comment_status',
        'value' => 'anyStringNotNullNotEmpty',
        'compare' => 'NOT EXISTS'
    )
    Thread Starter ScruffyDan

    (@dmoutal)

    Thanks, that didn’t help. ultimately adding ‘post_id’ => get_the_ID() is what did the trick. I have no idea why though since this code was always running as part of the loop

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘comment meta_query for keys that aren't yet set’ is closed to new replies.