logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Query string to show only posts with no meta value

Hi there,

I'm trying to list posts in a category that have not been set to 'featured', to go beneath a list of posts that have been set to 'featured'. I guess you could call it a list of 'everything else'.

This is the query string I have so far.

"&category_name=news&posts_per_page=5&meta_key=featured&meta_value=-"


It's not working out for me. Is there something I should use instead of '-' for the meta_value? That's worked for me elsewhere in my template, but just isn't doing the job here.

Thanks in advance!

This question has been answered.

Millions | 11/29/11 at 5:35am Edit


(9) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    11/29/11
    5:38am
    Utkarsh Kukreti says:

    Not sure if it'll work, but how about

    "&category_name=news&posts_per_page=5&meta_key=featured&meta_value="

    • 11/29/11 5:43am

      Millions says:

      This isn't working unfortunately, but thanks for the suggestion.

  • avatar
    Last edited:
    11/29/11
    5:40am
    Arnav Joy says:

    what is the meta value of featured post??

    • 11/29/11 5:44am

      Millions says:

      The value of the featured post is 'on' - it's a tick box.

      Sadly 'off' doesn't work...

    • 11/29/11 5:48am

      Arnav Joy says:

      try following:-

      "&category_name=news&posts_per_page=5&meta_key=featured&meta_value!='on' "

    • 11/29/11 6:59am

      Arnav Joy says:

      If you can provide me details of your page then i can solve this.
      my id is: robingupta0512@gmail.com

  • avatar
    Last edited:
    11/29/11
    5:42am
    Sébastien | French WordpressDesigner says:

    "&category_name=news&posts_per_page=5&meta_key=featured&meta_value="

    and if you use a query_posts in the precedent loop, don't forget to use wp_reset_query();
    at the end of this precedent loop

    Previous versions of this answer: 11/29/11 at 5:42am

    • 11/29/11 5:46am

      Millions says:

      That's not doing the trick I'm afraid.

    • 11/29/11 5:50am

      Sébastien | French WordpressDesigner says:

      is there a value by default for the meta_key "featured" ?

    • 11/29/11 6:15am

      Millions says:

      Just checked and unfortunately there isn't, no.

  • avatar
    Last edited:
    11/29/11
    5:42am
    Francisco Javier Carazo Gil says:

    Hi Millions,

    If what Utkarsh says is not working, I recommend you to do the next:
    1. Make a meta box to manage the metafield
    2. If featured is selected, then save featured, else save not_feauted for example
    3. Make your theme compatible with this change
    4. You have now a query string

    • 11/29/11 5:47am

      Millions says:

      Hi there, that would certainly be last resort. Unfortunately I have over 1000 posts that I'd need to set to 'not featured', so I'm hoping to do it with a query...

    • 11/29/11 5:52am

      Francisco Javier Carazo Gil says:

      Millions,

      You can do an UPDATE directly in SQL.

      UPDATE wp_postmeta
      SET meta_value = 'not_featured'
      WHERE meta_key = 'featured';

    • 11/29/11 6:14am

      Millions says:

      Thanks, I may end up doing this. It would be a shame to have to have to make all new stories 'not_featured' though - an extra thing to do when writing a story.

    • 11/29/11 6:16am

      Francisco Javier Carazo Gil says:

      Sorry Millions,

      You have to include another condition:

      UPDATE wp_postmeta
      SET meta_value = 'not_featured'
      WHERE meta_key = 'featured' AND meta_value != 'featured';

      That's OK

  • avatar
    Last edited:
    11/29/11
    5:49am
    Kannan C says:

    when you save the post meta, do like this

    if($_POST["featured_post"])
    update_post_meta($post->ID, "_featured_post", 'on');
    else update_post_meta($post->ID, "_featured_post", 'off');

    //then your query should be
    "&category_name=news&posts_per_page=5&meta_key=featured&meta_value=off"

    • 11/29/11 6:45am

      Kannan C says:

      if you already have posts then try this


      $query = new WP_Query( array( 'meta_key' => 'featured_post', 'meta_value' => 'on', 'meta_compare' => '!=' ) );

  • avatar
    Last edited:
    11/29/11
    6:04am
    Jurre Hanema says:

    Query strings suck. Can I see how you query your posts using your current query string (I suspect query_posts or WP_query)? I may then be able to rewrite it using an "advanced" meta query, which should do the job. (Are you using WP 3.1 or higher? If not, upgrade)

    • 11/29/11 6:11am

      Millions says:

      Here's how it looks at the moment...


      <?php
      $otherstuffQuery = $query_string . "&category_name=news&posts_per_page=5&meta_key=featured&meta_value=-";
      $myposts = get_posts( $otherstuffQuery );
      if(count($myposts) >0) {
      ?>

      <?php
      foreach( $myposts as $post ) : setup_postdata($post); ?>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
      <?php the_title(); ?>
      </a>

      <?php endforeach; ?>
      <?php
      } ?>


      And yes I'm on 3.1. Thanks in advance...

    • 11/29/11 7:03am

      Jurre Hanema says:

      Try this:


      <?php

      $otherstuffQuery = new WP_Query;

      $otherstuffQuery->query(
      array(
      'posts_per_page' => 5,
      'category_name' => 'news',
      'meta_query' => array(
      array(
      'key' => 'featured',
      'value' => 'on',
      'compare' => '!='
      )
      )
      )
      );


      while($otherstuffQuery->have_posts())
      {
      $otherstuffQuery->the_post();
      ?>
      <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
      <?php the_title(); ?>
      </a>
      <?php
      }

      wp_reset_query();
      ?>


      I didn't test it yet... but I think there's a good chance it might work.

  • avatar
    Last edited:
    11/29/11
    7:50am
    Luis Abarca says:

    Try this way


    "&category_name=news&posts_per_page=5&meta_key=featured&meta_value=on&meta_compare=!="


    Or this one (its better and recommended)


    <?php

    // based on http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
    $args = array(
    'posts_per_page' => 5,
    'category_name' => 'news',
    'meta_query' => array(
    array(
    'key' => 'featured',
    'value' => 'on',
    'compare' => '!=' // get posts with not "ON" value
    )
    )

    );

    // new wp_query object
    $wpquery2 = new WP_Query($args);


    // look for results
    if ( $wpquery2->have_posts() ) {
    // the loop
    while ( $wpquery2->have_posts() ) {
    // init post
    $wpquery2->the_post();
    ?>
    <h2><?php the_title() ?></h2>
    <?php
    }
    }

    unset($wpquery2);
    wp_reset_query();

    Previous versions of this answer: 11/29/11 at 7:47am | 11/29/11 at 7:50am

  • avatar
    Last edited:
    11/30/11
    1:58pm
    Manoj Raj says:

    i don't think meta_compare will work. I have maintained some records of situations i have come across in wordpress... Luckily One text file contains this code .. This solution provided by Joe will help you for sure.. This should be the way to solution..

    Hope this code is already self explanatory. if it is not, i will give you an example some time later.. Or some other guys here will help you..

    // add a filter to 'posts_where' to add the subquery
    add_filter( 'posts_where', '_exclude_meta_key_in_posts_where' );

    // make the query, the below function will be called
    query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC'));

    function _exclude_meta_key_in_posts_where( $where ) {

    global $wpdb;
    return $where . " AND $wpdb->posts.ID NOT IN ( SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = 'featured_product' AND meta_value > '' )";
    }

    //remove the filter incase we do any more query_posts()s
    remove_filter( 'posts_where', '_exclude_meta_key_in_posts_where' );



    Thank you

  • avatar
    Last edited:
    12/02/11
    1:33am
    Julio Potier says:

    Or again, try this :
    $newWPQuery = new WP_Query( array( 'meta_key' => 'featured_post', 'meta_value' => 'on', 'meta_compare' => '!=' );

This question has expired.



Millions voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.