WordPress.org

Ready to get started?Download WordPress

Forums

Edit Flow
[resolved] How to get custom statuses to show up in the archive? (6 posts)

  1. Yaron Guez
    Member
    Posted 1 year ago #

    Hi there,
    I'm trying to get a custom post type with custom post statuses to show up in the custom post type archive. However only posts with the status "published" show up.

    This is part of the purchase order tracking system that I'm building. The post type is called purchase_order. I've already modified the archive query to only show posts that were created by the logged in user but when I try to modify it to include all post statuses I still only get published posts. This is true even when I try replacing 'any' with 'draft' for example. The code is below.

    How can I include custom statuses in a post type archive? Thanks!

    add_action( 'pre_get_posts', 'load_purchase_orders' );
    function load_purchase_orders( $query )
    {
        $user = wp_get_current_user();
        if ( ! is_admin() && $query->is_main_query() && ! $query->is_post_type_archive('purchase_order'))
        {
            //match posts to the logged in user.  this works perfectly:
            $query->set( 'author', $user->ID );
    
           //display all posts regardless of post status.  this doesn't work:
            $query->set( 'post_status', 'any' );
    	}
    }

    http://wordpress.org/extend/plugins/edit-flow/

  2. Yaron Guez
    Member
    Posted 1 year ago #

    Nevermind. typos in my code. it works now! *headslap

  3. Yaron Guez
    Member
    Posted 1 year ago #

    Here's the working code for those who are curious:

    add_action( 'pre_get_posts', 'load_purchase_orders' );
    function load_purchase_orders( $query )
    {
        $user = wp_get_current_user();
    
        if ( !is_admin() && $query->is_main_query() && $query->is_post_type_archive('purchase_order'))
        {
            //show all purchase orders that are open
            $query->set( 'post_status', array('new-order','assigned-pm','assigned-xfer','in-progress','ready-for-editing','complete') );
            //only show those purchase orders that are created by the logged in user
            $query->set( 'author', $user->ID );
        }
    }
  4. Daniel Bachhuber
    Hand Built
    Plugin Author

    Posted 1 year ago #

    Thanks for sharing, yguez

  5. maher.naija@gmail.com
    Member
    Posted 1 year ago #

    Hi ever body pleae in witch file i hav to add this code thanks for help .

  6. Yaron Guez
    Member
    Posted 1 year ago #

    Hi Maher,
    Just put it in your functions.php file.
    Yaron

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic