Wordpress - Filtering posts by post meta data
The WP_Query
object accepts a post meta argument. Generally speaking you want to do the following:
$my_query = new WP_Query(
array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'project_cat',
'value' => 'my-value',
)
),
// Other query properties
)
);
Where 'my-value' is your 'specified value'.
Example usage:
add_action( 'pre_get_posts' , 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
// Check this is main query and other conditionals as needed
if( $query->is_main_query() ) {
$query->set(
'meta_query',
array(
array(
'key' => 'project_cat',
'value' => 'my-value'
)
)
);
}
}
See WP_Query, pre_get_posts. All conditionals are available to you. Currently this runs on every main query - which you probably don't want.
Alternatively you can use query_posts
(a simpler, but much less efficient way) to alter the query for only a specific instance in a template.