To get custom posts by meta field value:

                    $args = array(
                        'post_type'  => 'student_tc',
                        'meta_query' => array(
                            'relation' => 'OR',
                                    'key'     => 'meta_field/acf_field',
                                    'value'   => 'field_value',
                                    'key'     => 'meta_field/acf_field',
                                    'value'   => 'field_value',
                                    'key'     => 'meta_field/acf_field',
                                    'value'   => 'field_value',

                    $posts = new WP_Query( $args );

The Code

You’re building an array ($args) to define the arguments for a custom query.


‘post_type’ => ‘student_tc’: This specifies that you want to query posts of the custom post type student_tc.


1. ‘meta_query’ => array(…): This part defines an array of custom field queries. In this case, you’re looking to match posts that have specific meta field values.
2. ‘relation’ => ‘OR’: This means that the query will return posts that match any of the specified conditions in the meta_query array, not all of them.
3. The subsequent arrays define the conditions to match:
a. ‘key’ => ‘meta_field/acf_field’: Specifies the custom field key you’re looking for.
b. ‘value’ => ‘field_value’: Specifies the value of the custom field you want to match.

However, all three conditions in the meta_query array are identical, which means the query will behave the same as if only one condition was specified.

Querying Posts

$posts = new WP_Query($args): This line creates a new instance of the WP_Query class, passing in your $args array, and stores the resulting query object in the $posts variable.


You can then use the $posts object in a loop to display the posts that meet the conditions. For example:

if ( $posts->have_posts() ) {
    while ( $posts->have_posts() ) {
        // Output the post content here
} else {
    // No posts found



This code snippet enables you to query custom posts in WordPress based on specific custom field values using ACF. It’s a powerful way to tailor content retrieval based on custom metadata, enabling more dynamic and context-sensitive content on your site. Make sure to define unique key-value pairs in the meta_query array to utilize this capability fully.


Leave a Comment