0

I can't find a proper solution to this.

I have an array of categories for example:

$slidercategories_array = array(3,5,6,9,10,11,12,23,24,27); 

I need to reorder the array like this first the category with the most recent post, and last the category with the oldest post.

Any ideas will be helpfull.

1 Answer 1

0

You could try

$slidercategories_array = array(3,5,6,9,10,11,12,23,24,27);
$in = implode( ',', array_map( 'absint', $slidercategories_array ) );
global $wpdb;
$sql = $wpdb->prepare(
    'SELECT tt.term_id, MAX(p.post_date) AS max_date FROM ' . $wpdb->term_taxonomy . ' AS tt '
    . ' JOIN ' . $wpdb->term_relationships . ' AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id'
    . ' LEFT JOIN ' . $wpdb->posts . ' AS p ON (p.ID = tr.object_id AND p.post_type = %s AND p.post_status = %s)'
    . ' WHERE tt.term_id IN (' . $in . ') AND tt.taxonomy = %s'
    . ' GROUP BY tt.term_id'
    . ' ORDER BY max_date DESC'
    , 'post', 'publish', 'category' );
$slidercategories_sorted = $wpdb->get_col( $sql );
3
  • After a few weeks in use I have noticed that the second category stays always the same!! any ideas why? Dec 30, 2014 at 18:00
  • Can't think of anything off-hand. Have you tried assigning a new dummy post to the offending category to see if it moves to the front?
    – bonger
    Dec 30, 2014 at 21:17
  • Ignore the above comment. This was another error. Sorry to bother you... Dec 31, 2014 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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