WordPress.org

Ready to get started?Download WordPress

Forums

meta_query don't work on WP_Query - it's shows all posts (4 posts)

  1. wamslers
    Member
    Posted 5 months ago #

    Hi,

    Im trying to get an result with an "new WP_Query" - it works, but not with the meta_query values. It's results all posts.

    What i'm doing wrong!?

    Here s my code:

    $userid = $_GET['userid'];
    $visits1_id = $visits1['venid'];
    
    $visits_args = array(
              'post_type' => 'post' ,
              'post_status' => 'publish' ,
              'cat' => 129 ,
              'meta_query' => array(
                        array(
                                  'meta_key' => 'userid' ,
                                  'meta_value' => $visits1_id
                        ),
                        array(
                                  'meta_key' => 'placeid' ,
                                  'meta_value' => $placeid
                        ) )
              );
    
    $get_visits = new WP_Query( $visits_args );
    $visits1_count = $get_visits->post_count;
    
    if($visits1_count == 0){ $visit = ''; } else { $visit = 'ok'; }

    The last line gives an OK if there's some posts where the placeid and userid is like the strings.

  2. wpfan1000
    Member
    Posted 5 months ago #

    Have you echoed $userid and $visits1_id to see if they have values in them?

  3. Jesper van Engelen
    Member
    Posted 5 months ago #

    When using meta_query, the keys you're looking for are not meta_key and meta_value but key and value:

    [..]
    'meta_query' => array(
    	array(
    		'key' => 'userid' ,
    		'value' => $visits1_id
    	),
    	array(
    		'key' => 'placeid' ,
    		'value' => $placeid
    	)
    )
    [..]

    Please check out the WordPress Codex on using meta_query.

  4. wamslers
    Member
    Posted 5 months ago #

    Thank You @Jesper van Engelen - it works.

Reply

You must log in to post.

About this Topic