I'm using a custom post type and showing 20 of them per page on the homepage. I'm using QP_Query.

One custom field for the custom post type (called mixtapes) is a checkbox called "unreleased" (set to true/false), and its basically a post that i dont want to include in the loop.

So far I put a conditional if inside the while loop to display only if 'unreleased' isnt set to 'true', but the problem is its (obviously) not applying to the loop itself, so as a result... the pagination 'sees' 40 posts (2 pages) when really theres only 1 unreleased album (which should be 1 page, but 2 pages show up). on the 2nd page, is no results.

Likewise when i want to show the 'top 10' albums which ARE released, it actually shows only 9 because the one unreleased one hasnt been removed from the actual loop, only conditionally 'hidden' after the fact.

How might i work this out so the loop keeps track of the posts that have been removed given a certain custom_field condition?


1 Answer 1


Rather than querying all posts and then only displaying those that match the criteria, query for posts that match the criteria. WP_Query allows you to do just that with its meta-key/meta-value attributes.

For instance, to get posts where the custom field unreleased is to 'false', somewhere before the loop:

global $wp_query;
$customField = array( 'meta_key' => 'unreleased', 'meta_value' => 'false' );
$args = array_merge( $wp_query->query,$customField);
query_posts( $args );

This isn't the most effecient way of doing things, but it is the simplest. We merge to the meta key/value query with the existing query (this includes pagination arguments).

You'll have to decide if WP_Query and query_posts is the right thing to use here (if it's not the main query on the page then you should get using get_posts instead - it accepts the same parameters).

Note: If you decide to use get_posts or WP_Query (but not with query_posts which alters the main query) then you shouldn't be merging. (If you're not sure which you should be using see this, now officially, 'great answer' :D)

  • thanks!! by the way.. i thought wp_query was used for not the main loop. is that incorrect? is both wp_query and query_posts used for only the main loop and get_posts is any other type
    – Tallboy
    Commented Mar 19, 2012 at 17:27
  • one problem... im already using meta_key to sort by the number of views assigned to each mixtape. can i use both meta keys in the same loop?
    – Tallboy
    Commented Mar 19, 2012 at 17:29
  • $mixtapes = new WP_Query ( array ( 'post_type' => 'mixtapes', 'posts_per_page' => 10, 'orderby' => 'meta_value_num', 'meta_key' => 'views' ) );
    – Tallboy
    Commented Mar 19, 2012 at 17:29
  • 1
    See this Codex page (there is a subsection on multiple custom field handling). I've not tested, but meta_query should be able to be used alongside meta_key and (certainly) orderby. Commented Mar 19, 2012 at 18:58
  • 1
    Yes $wp_query and query_posts refer to the main loop. $wp_query is an WP_Query object. You can have other WP_Query objects though (i.e. other loops), but the 'main' loop will always be $wp_query. Commented Mar 19, 2012 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.