Usually we add additional meta fields to post or post types. Wordpress provides very flexible way to get the post/post types based on the meta fields and the values.
The get_posts and the WP_query follow the same structure for accepting arguments and the args mentioned below could be used for both of them.
If we want to display the post/post type based on a particular value of a meta key we could use the following code
The get_posts and the WP_query follow the same structure for accepting arguments and the args mentioned below could be used for both of them.
If we want to display the post/post type based on a particular value of a meta key we could use the following code
1: $args = array(
2: 'post_type' => 'post',//or the slug of your post type
3: 'meta_key' => 'the_meta_field_name',
4: 'meta_value' => 'the_meta_field_value',
5: );
The problem with the above approach is there is no option to add one more meta field. A more desirable or if you want a more complex result to display then the meta_query is better option.
The meta_query option gives option to add multiple options based on the multiple meta fields associated with the post or post type.
1: $args = array(
2: 'post_type' => 'post',//or slug of the post type
7: 'meta_query' => array(
8: array(
9: 'key' => 'the_meta_field_name',
10: 'value' => 'the_meta_field_value',
11: 'compare' => '='
12: ),
13: array(
14: 'key' => 'the_meta_field_another',
15: 'value' => '"%the_meta_field_another_value"',
16: 'compare' => 'LIKE'
17: ),
18: )
19: );
The the above array could be used with both get_posts and WP_query as per your need.