0

I am making a wordpress site to host my artwork and comics, each comic series is in its own category, so I would like to display the category information in the site through my theme/a plugin I make.

There are three things I would like to do:

  • Show the category Title, Description and a url to it's archive by using it's category ID.
  • Get the Ids of the oldest and newest post in that category so I can add links to "Start Reading" and "Read the latest"
  • Sort an array of category ids by the date of the latest post, so the most recently updated series can automatically float to the top of the list.

Is it possible to do this?

  • Yes, these all things are possible. – BlueSuiter Apr 1 at 10:47
  • Could you point me to the documentation so I can read about it? Im having trouble figuring out how to google it. – metichi Apr 1 at 10:59
  • What are the things you are done implementing? Or can I see your implementation? – BlueSuiter Apr 1 at 11:14
  • I haven't implemented anything about this yet since so far I'm trying to figure out what functions to call to get the information – metichi Apr 1 at 11:15
  • This one will fetch all the terms for you, developer.wordpress.org/reference/functions/get_terms – BlueSuiter Apr 1 at 11:17
1

Show the category title, description and a url to it's archive by using it's category ID:

Get the Ids of the oldest and newest post in that category:

// Get oldest post ID
$posts = get_posts( array(
    'category' => 1,
    'orderby' => 'date',
    'order' => 'ASC',
    'post_type' => 'post',
    'post_status' => 'publish', 
) );
$oldest_post_id = $posts[0]->ID;
// Get newest post ID
$posts = get_posts( array(
    'category' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    'post_type' => 'post',
    'post_status' => 'publish', 
) );
$newest_post_id = $posts[0]->ID;

Sort an array of category ids by the date of the latest post:

function get_categories_by_the_date() {
    $categories_by_the_date = array();
    $categories = get_categories();

    foreach ( $categories as $category ) {
        $posts = get_posts( array(
            'category' => $category->term_id,
            'orderby' => 'date',
            'order' => 'DESC',
            'post_type' => 'post',
            'post_status' => 'publish', 
        ) );
        $categories_by_the_date[strtotime($posts[0]->post_date)] = $category->term_id;
    }

    krsort($categories_by_the_date);

    return array_values($categories_by_the_date);
}
| improve this answer | |
  • Perfect, this is exactly what I needed, thank you so much! – metichi Apr 1 at 11:26

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.