Home > other >  Querying custom post type for all posts from logged in user
Querying custom post type for all posts from logged in user

Time:09-26

I'm trying to query a custom post type, for only the current logged in user.

My query runs, but it's returning records for ALL post_author's - not just the currently logged in one.

I have included Author in the setup of the custom post type (which is a POD):

[![PODAuthor][1]][1]

...and post_author is in the parent POST - however, Author is not in the metadata for the post (in post_meta):

[![postmeta][2]][2]

Does WP_Query not also query the posts table for author:

[![postauthor][3]][3]

Can anyone point me to what's wrong with my code below please?

Thanks, Mark

$user = wp_get_current_user();
$user_id = get_current_user_id();

$query_args = array(
  'post_type'             => 'tftracker',
  'post_author'           => $user_id,
  'order'                 => 'ASC',
  'orderby'               => 'meta_value',
  'meta_key'              => 'result_date',
  'nopaging'              => true,
'meta_query'       => array(
    array(
      'key'        => 'unit_type',
      'value'      => 'steps',
      'compare'    => '=',
    )
  )
);

$query = new WP_Query($query_args);


  [1]: https://i.stack.imgur.com/qwIGd.png
  [2]: https://i.stack.imgur.com/tNllE.png
  [3]: https://i.stack.imgur.com/gEadx.png

CodePudding user response:

post_author is not a valid parameter for WP_Query

author is the correct one.

For more information on author parameters, Please check this Reference

  • Related