I want to add number after post tittle for each category

I want to add number after post tittle for each category

There is one plugin called auto numbering posts whose function is to add a number next to the post title, but the drawback of this plugin is that it doesn’t sort by category. I want the posts in each category to have their own numbering order.

add_action('the_title', 'dk_auto_numbering');
function dk_auto_numbering($title)
{
    $post_ID = get_the_ID();
    $the_post = get_post($post_ID);
    $date = $the_post->post_date;
    $maintitle = $the_post->post_title;
    $count = '';
    if ($the_post->post_status == 'publish' and $the_post->post_type == 'post' and in_the_loop()) {
        global $wpdb;
        $count = $wpdb->get_var("SELECT count(*) FROM $wpdb->posts  WHERE post_status='publish' AND post_type='post' AND post_date<'{$date}'");
        if ($maintitle == $title) {
            $count = $count . '';
            $title = $title . ' – Chapter ';
        } else {
            $count = '';
        }
    }
    return $title . $count;
} 

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Here’s an untested edit of your code to use the post’s first category in the count:

add_action('the_title', 'dk_auto_numbering');
function dk_auto_numbering($title)
{
    $post_ID = get_the_ID();
    $the_post = get_post($post_ID);
    $date = $the_post->post_date;
    $maintitle = $the_post->post_title;
    if ($maintitle == $title && $the_post->post_status == 'publish' &&
        $the_post->post_type == 'post' && in_the_loop()) {
        $categories = get_the_category($post_ID);
        if (is_array($categories) && count($categories) >= 1) {
            $first_category_id = $categories[0]->term_id;

            global $wpdb;
            $count = $wpdb->get_var(
                $wpdb->prepare("SELECT count(*) FROM $wpdb->posts AS p " .
                    "INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = p.id AND tr.term_taxonomy_id = %d " .
                    "WHERE post_status='publish' AND post_type='post' AND post_date <= %s", $first_category_id, $date)
            );

            if (isset($count)) {
                return $title . ' – Chapter ' . $count;
            }
        }
    }
    return $title;
}

I’ve moved the has-something-else-already-modified-the-title check out to the top level, to run it before the database query, plus a few other minor rearrangements. It’s slightly asymmetric in that it uses the current post’s first category but counts any earlier post that has that category at all, but hopefully that’s enough for what you want: if you need any more complicated category handling you can edit the SQL.

As I said in my original comment though I think these counts ought to be pre-computed and cached against the post in post meta, rather than run as a separate query for every post title shown on the page.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x