<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => featured_product, 'meta_value' => 1)); ?>
<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC')); ?>
I have 2 queries, first to show meta key with featured_product eq 1. So I want to exclude all the featured products on the second query. How can i do that please? Thanks!
I’ve never had any luck getting the meta compare to work either–but I came up with a workaround for this exact situation (having “featured” items at the top of the page).
First, you probably shouldn’t be using query_posts for both queries. You should use a custom query for at least the first one. Then, while you’re running that loop, keep the ids of any “featured” posts in a variable. When you run your second loop, you can use the “post__not_in” arg to exclude the featured ones. Like so:
MathSmath’s answer is ok, but it’s very inefficient and would not scale to a WordPress site with 100,000+ posts or what have you. Doing a
WP_Query()
for 1000 posts is going to be slow enough.Rather than using a
posts_not_it
(which would pass a query long string if you hade several thousand posts here) if you have MySQL 4.1+ it would be better to use a subquery to exclude the posts you don’t want.Here is a slightly modified example I am using on a site with 250,000+ posts.
You could probably do it all in one
create_function()
though I am not sure if that is very efficient, but would be less lines:I didn’t test the
create_function()
example.I haven’t tried this, but some research let me to this. Using meta_compare you can check if meta_value does not equal 1 display the posts.
My solution was to add the custom field with a ‘false’ value to all of the existing posts and to hook the creation of new posts in order to add the field to them as well.
I’m not sure if this is a more efficient way to handle this situation but I wanted to stay away from custom SQL queries.
The following is a one-time-use function I used for adding the field to all existing posts: