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)