0

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'
                    )
                )
                )
            );

Any help would be appricieated.

Thanks

UPDATE: For the record this is the code that ultimately worked for me:

            $comments = get_comments( array( 
                'order' => 'ASC',
                'post_id' => get_the_ID(),
                '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',
                        'value' => ''
                    )
                )
            ) );

The key was to set 'post_id' => get_the_ID()

2
  • Please add your edit as answer and mark it as solution. Else this question will stay open forever.
    – kaiser
    Oct 18, 2013 at 7:07
  • Will do. Sorry I am new to stack exchange
    – ScruffyDan
    Oct 19, 2013 at 19:42

2 Answers 2

2

You have no need for three states. Logically, you have "set" and "unset". Don't complicate things. When you check the box, add p3_comment_status. When you uncheck, remove (delete) the key. If you do that, all you need to do is use EXISTS/NOT EXISTS. That makes for a much simpler, and faster, query as a bonus.

You have not posted the code that saves your data so a detailed answer is not possible but delete_comment_meta is probably what you need.

6
  • Thanks, that does seem less complex. But something is wrong somewhere and I am not sure what it could be. I can execute a query with 'compare' => 'EXISTS' and it works as expected, but if I try 'compare' => 'NOT EXISTS' I get the following error: Fatal error: Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0
    – ScruffyDan
    Oct 17, 2013 at 19:48
  • making some progress here. Not sure why but the Fatal error is solved by adding 'post_id' => get_the_ID() to get-comments. although simply removing the meta key is simpler this is more robust incase for whatever reason the meta key isn't deleted. Thanks
    – ScruffyDan
    Oct 17, 2013 at 21:56
  • I don't understand the output buffering error. There is no output buffering in your code.
    – s_ha_dum
    Oct 17, 2013 at 22:22
  • nice to know I am not the only one that is confused:) for the record I updated my answer to include the code that ultimately worked for me
    – ScruffyDan
    Oct 18, 2013 at 1:20
  • 1
    It is only more robust if your "save" code is bad and it is always less efficient and slower.
    – s_ha_dum
    Oct 18, 2013 at 13:55
0

For the record this is the code that ultimately worked for me:

            $comments = get_comments( array( 
                'order' => 'ASC',
                'post_id' => get_the_ID(),
                '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',
                        'value' => ''
                    )
                )
            ) );

The key was to set 'post_id' => get_the_ID()

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.