Forums

get_posts fails to return drafts and published posts (3 posts)

  1. GrahamW
    Member
    Posted 8 months ago #

    Hi,
    I can't get the get_posts function to return both drafts and published posts. This is my code:

    $args = array(
    	'numberposts'	=> NULL,
        'offset'		=> 0,
        'orderby'		=> 'title',
        'order'			=> 'ASC',
        'post_type'		=> 'post',
    	'post_status'	=> array('drafts', 'publish')
    	);
    $Post_Array = get_posts($args);

    If I run the function twice and combine the resulting arrays it works, but then the orderby is wrong because it just adds the second array to the end of the first array. Here is the code:

    $args = array(
    $args = array(
    	'numberposts'	=> NULL,
        'offset'		=> 0,
        'orderby'		=> 'title',
        'order'			=> 'ASC',
        'post_type'		=> 'post',
    	'post_status'	=> 'draft'
    	);
    $Post_Array1 = get_posts($args);
    
    $args = array(
    	'numberposts'	=> NULL,
        'offset'		=> 0,
        'orderby'		=> 'title',
        'order'			=> 'ASC',
        'post_type'		=> 'post',
    	'post_status'	=> 'publish'
    	);
    $Post_Array2 = get_posts($args);
    
    $Post_Array = array_merge((array)$Post_Array1, (array)$Post_Array2);
  2. alchymyth
    The Sweeper
    Posted 8 months ago #

    typing error?

    'post_status'	=> array('drafts', 'publish')

    make sure it says 'draft' in your real code...

  3. GrahamW
    Member
    Posted 8 months ago #

    Thank you!!!

    I must have mistyped that at least 3 times. lol
    I think I need a break lol

    Regards,
    Graham

Reply

You must log in to post.

About this Topic