0

I try to get posts that either do not have a custom field set or if set have a value not like a given parameter.

Here is my code:

    $args_included = array(
        'numberposts'     => 1,
        'post_type'       => 'post',
        'post_status'     => 'published',
        'meta_query' => array(
               'relation' => 'OR',
                array(
                    'key' => 'requested_by',
                    'value' => '%' . $SANITIZED_PARAM . '%',
                    'compare' => 'NOT LIKE'
                ),
                array(
                    'key' => 'requested_by',
                    'compare' => 'NOT EXISTS'
                )
        )
    );

    get_posts($args_included); // empty array

The custom field requested_by is supposed to be either empty or filled with a string value like this one : foo;bar;gamma;.

If I remove this part of the code :

                'relation' => 'OR',
                array(
                    'key' => 'requested_by',
                    'value' => '%' . $SANITIZED_PARAM . '%',
                    'compare' => 'NOT LIKE'
                ),

the query then works.

What am I missing ?

WP 3.5

UPDATE Here is my final code that work :

    $args_included = array(
        'numberposts'     => 1,
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'meta_query' => array(
                array(
                    'key' => 'requested_by',
                    'value' => $PARAM,
                    'compare' => 'NOT LIKE'
                )
        )
    );

    get_posts($args_included); // Just ONE post

Mistakes:

  • Status is publish not published
  • The custom field didn't exist so no results can be found.
    Below is a SQL query for mass setting the custom field:

    INSERT INTO wp_postmeta(post_id,meta_key,meta_value)
       SELECT ID , 'requested_by', ';'
       FROM wp_posts
       WHERE post_status='publish'
       AND post_type='post'
    
  • Last tips for debug purpose. You can get the final SQL query sent to MySQL like this:

    $args = array(
        'numberposts'     => 1,
        'post_type'       => 'post',
        'post_status'     => 'publish',
        'meta_query' => array(
                array(
                    'key' => 'requested_by',
                    'value' => $PARAM,
                    'compare' => 'NOT LIKE'
                )
        )
    );
    
    $q = new WP_Query($args);
    echo $q->request;
    

1 Answer 1

2

You don't need to add the % signs to the value. WordPress does that for you automatically on a LIKE or NOT LIKE comparison. You also don't need to "sanitize" the parameter, it will be automatically escaped for SQL.

See wp-includes/meta.php, around line 806 or so.

3
  • I have updated my code but the problem remains :\
    – Stephan
    Commented Dec 26, 2012 at 18:48
  • 1
    Instead of using get_posts, make a new WP_QUery() with your arguments, then var_dump() the query. This will give you, among other things, the SQL it's generating. This will help see what's wrong with your parameters.
    – Otto
    Commented Dec 26, 2012 at 19:32
  • Oh, also, "post_status" should be "publish", not "published".
    – Otto
    Commented Dec 26, 2012 at 19:33

Your Answer

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

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