• Hi everybody,

    Totally new on the wordpress forums but been using wordpress for a long time.

    I have tried both get_posts & query_posts in order to get all posts that doesn’t have a certain meta_key set.
    How would I do this?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Not sure what you mean by ‘set’, but what about:

    $args=array(
      'meta_key' => 'your_custom_field',
      'meta_value' => 'your_custom_value',
      'meta_compare' => '!=",
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);

    Note: wonder what happened to my first response to this thread?

    Thread Starter theindustry

    (@theindustry)

    Hi Michael,
    Thanks for your answer.

    Unfortunately that code does only fetch posts where the meta_value isn’t “your_custom_value”, meaning that a post that doesn’t have the “your_custom_field” meta_key won’t show up.

    Is there any way I can do this?

    I think you’d need two queries, one to fetch the post ids of posts with the given key, then a second query to pull posts, excluding the IDs passed in from the first query..

    It won’t be an efficient query, but it would work.. (potentially large query if you have hundreds/thousands of records).

    theindustry, how’s your PHP / MySQL skills? Considered writing a query to do it? Might work with a series of filters, but what’s this code for? Are you writing this code into a plugin/theme or is this more a case of trying to find redundant records?

    Thread Starter theindustry

    (@theindustry)

    Hi Mark,

    Well, I have a fair share of experience in writing my own queries, but really no idea of how the structure looks in WordPress regarding posts&their meta.

    Do you have any tips on where I might start look?

    Thanks

    Didn’t test this but, this queries all posts, and then loops through each post and if the custom field doesn’t exist display the post title:

    <?php
    $args=array(
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
        $custom = get_post_meta($my_query->post->ID, 'yourcustomfield', true);
        if ( ! $custom ){
        ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
          <?php
        }
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Hey, I like that!

    I’ve been doing million awkward work arounds to achieve the same thing. I might have to switch over. Thanks!

    PS since that’s all outside the loop, where would you suggest pasting that code, in the theme functions.php?

    Thanks for your expertise Michael!

    Depends on where you want to ‘see’ that displayed. See Template Hierarchy.

    @theindustry

    The database schema can be seen here (i personally tend to just look at the structure in phpmyadmin).
    http://codex.wordpress.org/Database_Description

    Building your own queries is really just a matter of getting to know the $wpdb class, you can find info on that here.
    http://codex.wordpress.org/Function_Reference/wpdb_Class

    I quickly wrote something, as an example using $wpdb to query for posts without a given meta key.
    http://wordpress.pastebin.com/kgLt1RrG

    You’re welcome to use or the refine that code as much as you like and the commented section at the bottom is again, just another example.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get posts that DOESN'T have meta-keys?’ is closed to new replies.