[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

How to Get All Posts with any post status?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8873
    David Hoang
    Keymaster

    I am creating a front end dashboard where I need to show all the posts by the current user. So, I need to show posts in all states, mainly publishedtrashed and the pending. I am now using a simple query but it is returning only the published posts.

    $query = array(
        'post_type' => 'my-post-type',
        'post_author' => $current_user->ID              
        );
        query_posts($query);
    

    Can anyone help? What else do I need to do?

    #8875
    David Hoang
    Keymaster

    You can use the post_status parameter:

    * 'publish' - a published post or page
    * 'pending' - post is pending review
    * 'draft' - a post in draft status
    * 'auto-draft' - a newly created post, with no content
    * 'future' - a post to publish in the future
    * 'private' - not visible to users who are not logged in
    * 'inherit' - a revision. see get_children.
    * 'trash' - post is in trashbin. added with Version 2.9. 

    I’m not sure that it accepts ‘any’ so use an array with all of the statuses you want:

    $args = array(
        'post_type' => 'my-post-type',
        'post_author' => $current_user->ID,
        'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')    
    );
    $query = new WP_Query($args);
    
    while ( $query->have_posts() ) : $query->the_post();
    • This reply was modified 5 months, 2 weeks ago by David Hoang.
    #8876
    David Hoang
    Keymaster

    There is simple way, how to get all posts with any status:

    $articles = get_posts(
     array(
      'numberposts' => -1,
      'post_status' => 'any',
      'post_type' => get_post_types('', 'names'),
     )
    );

    Now you can iterate throughout all posts:

    foreach ($articles as $article) { 
     echo $article->ID . PHP_EOL; //...
    }
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.