Sunday, November 6, 2022
HomeWordPress Developmentwp question - WP_Query by meta key not returning any posts

wp question – WP_Query by meta key not returning any posts


I’ve set a customized meta key ‘post_views’ on a customized put up sort which I can see in my database has been added for 3 posts.

enter image description here

I now have to retrieve the ID’s for these posts by looking for all posts which have this meta key.

$q = new WP_Query([
    'meta_key'   => 'post_views',
]);

$posts = $q->posts;

foreach($posts as $put up) {
    var_dump($put up);
    echo $put up;
}

My downside is that once I do the WP_Query it doesn’t return any posts.

Can anybody see why im not getting any outcomes?

Thanks you

Additionally if I var_dump($q) it returns the next:

object(WP_Query)[12961]
  public 'question' => 
    array (measurement=1)
      'meta_key' => string 'post_views' (size=10)
  public 'query_vars' => 
    array (measurement=64)
      'meta_key' => string 'post_views' (size=10)
      'error' => string '' (size=0)
      'm' => string '' (size=0)
      'p' => int 0
      'post_parent' => string '' (size=0)
      'subpost' => string '' (size=0)
      'subpost_id' => string '' (size=0)
      'attachment' => string '' (size=0)
      'attachment_id' => int 0
      'title' => string '' (size=0)
      'pagename' => string '' (size=0)
      'page_id' => int 0
      'second' => string '' (size=0)
      'minute' => string '' (size=0)
      'hour' => string '' (size=0)
      'day' => int 0
      'monthnum' => int 0
      '12 months' => int 0
      'w' => int 0
      'category_name' => string '' (size=0)
      'tag' => string '' (size=0)
      'cat' => string '' (size=0)
      'tag_id' => string '' (size=0)
      'creator' => string '' (size=0)
      'author_name' => string '' (size=0)
      'feed' => string '' (size=0)
      'tb' => string '' (size=0)
      'paged' => int 0
      'meta_value' => string '' (size=0)
      'preview' => string '' (size=0)
      's' => string '' (size=0)
      'sentence' => string '' (size=0)
      'title' => string '' (size=0)
      'fields' => string '' (size=0)
      'menu_order' => string '' (size=0)
      'embed' => string '' (size=0)
      'category__in' => 
        array (measurement=0)
          empty
      'category__not_in' => 
        array (measurement=0)
          empty
      'category__and' => 
        array (measurement=0)
          empty
      'post__in' => 
        array (measurement=0)
          empty
      'post__not_in' => 
        array (measurement=0)
          empty
      'post_name__in' => 
        array (measurement=0)
          empty
      'tag__in' => 
        array (measurement=0)
          empty
      'tag__not_in' => 
        array (measurement=0)
          empty
      'tag__and' => 
        array (measurement=0)
          empty
      'tag_slug__in' => 
        array (measurement=0)
          empty
      'tag_slug__and' => 
        array (measurement=0)
          empty
      'post_parent__in' => 
        array (measurement=0)
          empty
      'post_parent__not_in' => 
        array (measurement=0)
          empty
      'author__in' => 
        array (measurement=0)
          empty
      'author__not_in' => 
        array (measurement=0)
          empty
      'ignore_sticky_posts' => boolean false
      'suppress_filters' => boolean false
      'cache_results' => boolean true
      'update_post_term_cache' => boolean true
      'update_menu_item_cache' => boolean false
      'lazy_load_term_meta' => boolean true
      'update_post_meta_cache' => boolean true
      'post_type' => string '' (size=0)
      'posts_per_page' => int 9
      'nopaging' => boolean false
      'comments_per_page' => string '50' (size=2)
      'no_found_rows' => boolean false
      'order' => string 'DESC' (size=4)
  public 'tax_query' => 
    object(WP_Tax_Query)[12964]
      public 'queries' => 
        array (measurement=0)
          empty
      public 'relation' => string 'AND' (size=3)
      protected 'table_aliases' => 
        array (measurement=0)
          empty
      public 'queried_terms' => 
        array (measurement=0)
          empty
      public 'primary_table' => string 'wp_posts' (size=8)
      public 'primary_id_column' => string 'ID' (size=2)
  public 'meta_query' => 
    object(WP_Meta_Query)[12963]
      public 'queries' => 
        array (measurement=2)
          0 => 
            array (measurement=1)
              ...
          'relation' => string 'OR' (size=2)
      public 'relation' => string 'AND' (size=3)
      public 'meta_table' => string 'wp_postmeta' (size=11)
      public 'meta_id_column' => string 'post_id' (size=7)
      public 'primary_table' => string 'wp_posts' (size=8)
      public 'primary_id_column' => string 'ID' (size=2)
      protected 'table_aliases' => 
        array (measurement=1)
          0 => string 'wp_postmeta' (size=11)
      protected 'clauses' => 
        array (measurement=1)
          'wp_postmeta' => 
            array (measurement=5)
              ...
      protected 'has_or_relation' => boolean false
  public 'date_query' => boolean false
  public 'queried_object' => *uninitialized*
  public 'queried_object_id' => *uninitialized*
  public 'request' => string '
                    SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID
                    FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
                    WHERE 1=1  AND ( 
  wp_postmeta.meta_key = 'post_views'
) AND ((wp_posts.post_type="put up" AND (wp_posts.post_status="publish")))
                    GROUP BY wp_posts.ID
                    ORDER BY wp_posts.post_date DESC
                    LIMIT 0, 9
                ' (size=352)
  public 'posts' => 
    array (measurement=0)
      empty
  public 'post_count' => int 0
  public 'current_post' => int -1
  public 'in_the_loop' => boolean false
  public 'put up' => *uninitialized*
  public 'feedback' => *uninitialized*
  public 'comment_count' => int 0
  public 'current_comment' => int -1
  public 'remark' => *uninitialized*
  public 'found_posts' => int 0
  public 'max_num_pages' => int 0
  public 'max_num_comment_pages' => int 0
  public 'is_single' => boolean false
  public 'is_preview' => boolean false
  public 'is_page' => boolean false
  public 'is_archive' => boolean false
  public 'is_date' => boolean false
  public 'is_year' => boolean false
  public 'is_month' => boolean false
  public 'is_day' => boolean false
  public 'is_time' => boolean false
  public 'is_author' => boolean false
  public 'is_category' => boolean false
  public 'is_tag' => boolean false
  public 'is_tax' => boolean false
  public 'is_search' => boolean false
  public 'is_feed' => boolean false
  public 'is_comment_feed' => boolean false
  public 'is_trackback' => boolean false
  public 'is_home' => boolean true
  public 'is_privacy_policy' => boolean false
  public 'is_404' => boolean false
  public 'is_embed' => boolean false
  public 'is_paged' => boolean false
  public 'is_admin' => boolean false
  public 'is_attachment' => boolean false
  public 'is_singular' => boolean false
  public 'is_robots' => boolean false
  public 'is_favicon' => boolean false
  public 'is_posts_page' => boolean false
  public 'is_post_type_archive' => boolean false
  non-public 'query_vars_hash' => string '2f64db49c5662c85abfbc42ba61e6b0c' (size=32)
  non-public 'query_vars_changed' => boolean false
  public 'thumbnails_cached' => boolean false
  protected 'allow_query_attachment_by_filename' => boolean false
  non-public 'stopwords' => null
  non-public 'compat_fields' => 
    array (measurement=2)
      0 => string 'query_vars_hash' (size=15)
      1 => string 'query_vars_changed' (size=18)
  non-public 'compat_methods' => 
    array (measurement=2)
      0 => string 'init_query_flags' (size=16)
      1 => string 'parse_tax_query' (size=15)

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments