0

I've got a function that pulls the latest post from each category and arrange categories by latests posts. What I get from this is an array with category ids which I want to use to rearrange the get_categories array.

/* Get all categories */
$categories = get_categories();
/* Create empty array */
$categories_order = [];
/* For each category */
foreach($categories as $category) {
  /* Modify WP query */
  $args = array('posts_per_page' => 1, /* Max 1 post */
                'category__in' => array($category->term_id), /* In this specific category */
                'ignore_sticky_posts' => true ); /* No sticky posts */
  /* Get all posts from categories with modifier */
  $posts = get_posts($args);
  /* If there are posts */
  if ($posts) {
    /* For each post */
    foreach($posts as $post) {
      /* Add to array key => value (category id => time published) */
      $categories_order[$category->term_id] = get_post_time('YmdHis');
    }
  }
}
arsort($categories_order); /* Order new array by value */
$categories_order = array_keys($categories_order); /* Remove array values */
print_r($categories_order);

My function returns:

Array ( [0] => 2 [1] => 5 [2] => 4 )

get_categories returns:

Array
(
    [1] => stdClass Object
        (
            [term_id] => 2
            [name] => Спорт
            [slug] => sport
            [term_group] => 0
            [term_taxonomy_id] => 2
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 6
            [cat_ID] => 2
            [category_count] => 6
            [category_description] => 
            [cat_name] => Спорт
            [category_nicename] => sport
            [category_parent] => 0
        )

    [2] => stdClass Object
        (
            [term_id] => 4
            [name] => Дом и градина
            [slug] => home-and-garden
            [term_group] => 0
            [term_taxonomy_id] => 4
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 4
            [category_count] => 1
            [category_description] => 
            [cat_name] => Дом и градина
            [category_nicename] => home-and-garden
            [category_parent] => 0
        )

    [3] => stdClass Object
        (
            [term_id] => 5
            [name] => Транспорт
            [slug] => transport
            [term_group] => 0
            [term_taxonomy_id] => 5
            [taxonomy] => category
            [description] => 
            [parent] => 0
            [count] => 1
            [cat_ID] => 5
            [category_count] => 1
            [category_description] => 
            [cat_name] => Транспорт
            [category_nicename] => transport
            [category_parent] => 0
        )

)

Now somehow get_categories term_id should be compared to my array and the objects rearranged accordingly.

0

Long story short, build a new array with the categories indexed by ID. Then you can just loop over the date-sorted IDs and do with them as you wish:

$categories = get_categories();
$cats_index =
$cats_dates = array();

foreach( $categories as $category ) {
    // Index the categories by their ID
    $cats_index[ $category->term_id ] = $category;

    $posts = get_posts(
        array(
            'ignore_sticky_posts' => true,
            'posts_per_page' => 1,
            'category__in' => array( $category->term_id ),
        )
    );

    if ( $posts )
        $cats_dates[ $category->term_id ] = ( int ) mysql2date( 'U', $posts[0]->post_date_gmt );
    else
        $cats_dates[ $category->term_id ] = 0;
}

arsort( $cats_dates );

foreach ( array_keys( $cats_dates ) as $cat_id ) {
    // Loop over category IDs by date descending
    $category = $cats_index[ $cat_id ];
}
0

usort uses a callback function to sort an array:

$categories_order = array_flip($categories_order);
$categories = usort( get_categories(), function( $a, $b ) {
    return $categories_order( $a['term_id'] ) - $categories_order( $b['term_id'] );
}
  • This doesn't seem to work. It's missing a closing bracket and semicolon. Even with that it returns PHP error "Fatal error: Function name must be a string in...". I had my eye on usort earlier, but even after reading on PHP docs I still can't seem to comprehend how the function actually works. – Georgi Demirev Aug 26 '15 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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