wordpressmeta-keypost-meta

Wordpress query custom meta key


I've added a postmeta (popular_posts) see image below. But when I query posts with meta key "popular_posts" like in below I've had no result:

new WP_Query(array( 'meta_key'=>'popular_posts' ))

Some one can explain me how to properly retrieve that have meta key "popular_posts" ?

enter image description here


Solution

  • This is the simple way to get post by their meta.

    $myquery = new WP_Query( "post_type=post&meta_key=popular_posts");
    

    Or You can use this :

    $second_loop = get_posts( array(
      'meta_key'   => 'popular_posts',
      'meta_value !=' => '',
    

    ) );