Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

Basically I have an array of posts which I want to sort by title.

$categories = get_categories('child_of=4'); 
$arr_records = array();
$post_data = array();

foreach($categories as $category) 
{
    $sub_catname = $category->cat_name;
    foreach(get_posts('cat='.$category->term_id) as $post) 
    {
        $arr_records[] = $post_id;
        $post_data[] = $post;
    }
}

foreach($post_data as $show_post)
{
     echo $show_post;
}

How do I sort the $post_data array?

share|improve this question

marked as duplicate by kaiser, s_ha_dum, toscho Aug 20 '13 at 7:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Please also take a look at the linked post by scribu and Mike Schinkels code. –  kaiser Aug 20 '13 at 3:09
add comment

1 Answer 1

Use the orderby and order parameters in the argument list. See the Orderby_Parameters section of WP_Query for details.

$category_ids = $arr_records = $post_data = array();

$categories = get_categories( 'child_of=4' );
foreach( $categories as $category ) {
    $category_ids[] = $category->term_id;
}

$post_data = get_posts( array(
    'cat'     => $category_ids,
    'orderby' => 'title',
    'order'   => 'ASC',
) );

$post_data should now be sorted by post title [A-Z].

share|improve this answer
    
Hi This doesn't work. I want to sort the $post_data[] array by title and display –  Azad Rana Aug 20 '13 at 1:22
    
See the the edit. –  Charles Clarkson Aug 20 '13 at 2:54
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.