WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 was marked as an exact duplicate of an existing question.

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

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

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