Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query string to show only posts with no meta value WordPress

  • SOLVED

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 <em>have</em> 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!

Answers (9)

2011-11-29

Francisco Javier Carazo Gil answers:

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


Millions comments:

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...


Francisco Javier Carazo Gil comments:

Millions,

You can do an UPDATE directly in SQL.

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


Millions comments:

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.


Francisco Javier Carazo Gil comments:

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

2011-11-29

Luis Abarca answers:

Try this way


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


<strong>Or this one (its better and recommended)</strong>


<?php

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

// 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();

2011-11-29

Utkarsh Kukreti answers:

Not sure if it'll work, but how about

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


Millions comments:

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

2011-11-29

Arnav Joy answers:

what is the meta value of featured post??


Millions comments:

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

Sadly 'off' doesn't work...


Arnav Joy comments:

try following:-

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


Arnav Joy comments:

If you can provide me details of your page then i can solve this.
my id is: [email protected]

2011-11-29

Sébastien | French WordpressDesigner answers:

"&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


Millions comments:

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


Sébastien | French WordpressDesigner comments:

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


Millions comments:

Just checked and unfortunately there isn't, no.

2011-11-29

Kannan C answers:

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"


Kannan C comments:

if you already have posts then try this

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

2011-11-29

Jurre Hanema answers:

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)


Millions comments:

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...


Jurre Hanema comments:

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.

2011-11-30

Manoj Raj answers:

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 <strong>Joe </strong>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

2011-12-02

Julio Potier answers:

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