• I am trying to produce a page that gives me a list of posts which haven’t yet had a certain custom value entered. I am using this page as I guide, but can’t seem to manage it. I can produce a list of post that DO have a value entered using:

    AND wpostmeta.meta_key = ‘custom_key’
    AND wpostmeta.meta_value > ”

    or even removing that second line completely. However I cannot get it to work in reverse. I have tried at least twenty variations of NOT and ! in various places and calling for either ” or NULL, but nothing does the trick.

    I think the problem lies in the fact that I am trying to retrieve posts where the ‘custom_key’ hasn’t yet been defined. If I define the custom key as blank, I can manage to pull a list of the posts where the value is blank, but this isn’t what I want. I want the posts where I haven’t done anything yet.

    I sense that the solution lies in calling all the posts where a value has been entered, and then saying ‘every other post’. Unfortunately I don’t know how to do this. Any ideas?

    Cheers

Viewing 4 replies - 1 through 4 (of 4 total)
  • If you are testing *against* a specific value existing in the meta_value column, try:

    AND wpostmeta.meta_value <> 'VALUE'

    Replace VALUE with yours.

    Thread Starter doppledoer

    (@doppledoer)

    No, I’m not testing against a specific value. I just want a list of the posts where I haven’t entered a value at all, so I know which need attending to.

    Thread Starter doppledoer

    (@doppledoer)

    In reality, I want a list of posts where the column ‘custom_key’ does not exist. However if I write:

    AND NOT wpostmeta.meta_key = ‘custom_key’

    it returns an entry for every column in every post that isn’t ‘custom_key’, far from what want. I’d like to call a unique list of every post and then eliminate all the ones that have a ‘custom_key’ column.

    Thread Starter doppledoer

    (@doppledoer)

    In case anyone else arrives here with the same problem, here’s the solution: Adapt this template to suit your various needs, but replace the database call with:

    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts
        WHERE wposts.ID NOT IN
          (
          SELECT wpostmeta.post_id
          FROM $wpdb->postmeta wpostmeta
          WHERE wpostmeta.meta_key = 'YOUR_KEY'
          )
        AND wposts.post_status = 'publish'
        AND wposts.post_type = 'post'
        AND wposts.post_date < NOW()
        ORDER BY wposts.post_title ASC
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Display posts which lack a certain custom value’ is closed to new replies.