Query custom field on custom post

Support General Query custom field on custom post

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #44579
    Marco NascimentoMarco Nascimento
    Participant

    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!

    #44586
    PeterPeter
    Moderator

    Hello Marco,

    Please try to use the key compare in the meta-query to see how it goes.

    '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

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.