Meta Box
Support › General › Query custom field on custom post
Hi there,
I'm trying to retrieve a set of 'custom_type' posts with a 'custom_field' condition. I'm not using a custom table in this case. This is the code I'm using.
$args = array( 'post_type' => 'custom_type', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'custom_field', 'value' => $value, ), ), ); $query = new WP_Query( $args );
But the query is returning an empty set. Could you please help me to fix this?
Thanks!
Hello Marco,
Please try to use the key compare in the meta-query to see how it goes.
compare
'meta_query' => array( array( 'key' => 'custom_field', 'value' => $value, 'compare' => '=' ), ),
Also, please ensure the post type slug custom_type, field ID custom_field are correct. Following the WordPress documentation https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
custom_type
custom_field