WordPress.org

Ready to get started?Download WordPress

Forums

comment meta_query for keys that aren't yet set (3 posts)

  1. dmoutal
    Member
    Posted 10 months ago #

    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

  2. x500.net
    Member
    Posted 10 months ago #

    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'
    )
  3. dmoutal
    Member
    Posted 10 months ago #

    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

Reply

You must log in to post.

About this Topic