• userpankaj

    (@userpankaj)


    Hey,

    I have a blog where i have more than 1000 articles. In every article, i have month and year. I want to change the month every new month and year every new year automatically.

    It will become difficult for me to manually update them as the number of articles is very high and will increase in the future. I am in a niche where it is necessary to give month and year.

    Right now, i developed a code that is changing the month and year but only in the front end. I want the month and year should also change in the backend as well so that it will change in the Google SERPs.

    The current format of my titles are:

    Example [January 2024 Updated]

    Example [January 2024]

    Please any solution of this is really appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    If I understand your desire correctly, you wish to append a sort of date stamp to titles as they are published or updated, reflecting the current month/year. Other than publishing or updating, the date does not change. IoW the title would not reflect the current month/year even though it was last updated months ago.

    The ‘wp_insert_post_data’ filter can be used to alter any post data just before it is inserted or updated, so this date stamp can be appended to the title here. You could even update the slug if you wanted to, but I would not recommend doing so.

    TBH, I don’t even recommend appending a date stamp to the title prior to saving. I very much dislike redundant data. The date already exists in the post record, it does not need to be repeated in the DB. I think appending it on output would be the better solution. But it’s your site, do as you wish ??

    Thread Starter userpankaj

    (@userpankaj)

    Thanks for replying. If i understand your reply correctly, you suggested that the month and year will be automatically inserted after the title after publishing or updating the old posts, right? Pardon me if i am wrong here.

    I already have 1000 articles having month and year in titles. I want some code that will change the month and year in already published articles as well as in future articles too as they are published in the backend without manually updating them.

    Right now, i am using this code generated by ChatGPT (as i don’t know coding much) which only changes the month and title in the front. But the month and year are not changing in Google SERPs.

    function update_article_title_month_year($title, $id = null) {
        if ((is_home() || is_front_page() || is_single() || is_archive()) && in_the_loop() && is_main_query()) {
            $current_month = date('F');
            $current_year = date('Y');
    
            // Use a regular expression to find and replace the [Month Year] or [Month Year Updated] pattern
            $pattern = '/\[(January|February|March|April|May|June|July|August|September|October|November|December) (\d{4})( Updated)?\]/';
            $replacement = function ($matches) use ($current_month, $current_year) {
                // Add "Updated" only if it was originally present in the title
                return "[$current_month $current_year" . (isset($matches[3]) ? ' Updated' : '') . "]";
            };
            $title = preg_replace_callback($pattern, $replacement, $title);
        }
    
        return $title;
    }
    
    add_filter('the_title', 'update_article_title_month_year', 10, 2);

    This code is changing month and year in the front end. I just want to know, is it possible to change the month and title in the backend as well automatically without hitting the ‘Update’ button?

    Here is the image of how i write me titles https://ibb.co/Cvkw38T

    Moderator bcworkz

    (@bcworkz)

    The existing posts and future posts are two independent issues that need to be dealt with separately. Do existing posts already have month/year data in titles, but they are incorrect? Or do they lack month/year data and you wish to add this?

    Where is the desired month/year data coming from? Can we use the post_date or post_modified fields of each post to supply the data? There is likely some fancy update query that can modify existing posts. But I don’t know how to compose such a thing. I would resort to using PHP to loop through a bunch of posts and update each as needed. PHP will likely time out before it can do all 1000, but it could likely do a few hundred at a time. As long as it can be discerned which posts are updated and which still need it, you could simply run the script a number of times until there are no posts left that need updating.

    Month/year data can certainly be appended to titles when initially published or when updated. The full title, month and year would all be saved in the DB so the ChatGPT suggested approach would not be necessary.

    This is not the approach I would use, but it can be done if that’s what you want. What I would do is leave titles alone without month/year and rely upon post_date or post_modified fields to dynamically append month/year to titles as they are output. I would do it this way because I despise redundant data. The month/year already exists in post records, why redundantly save it in the title as well when we can dynamically add it on output?

    I’ve no experience with ChatGPT, but I would think if it’s not giving you the code you want, you could modify your prompt until it delivers what you want.

    Thread Starter userpankaj

    (@userpankaj)

    The month/year in the title is already present because i wrote them at the time of publishing the article, you can see it in the image here:- https://ibb.co/vjWPypf

    I manually write month/year in every article while publishing them.

    Moderator bcworkz

    (@bcworkz)

    Thank you, it’s possible to change those and automatically append month/date any time a post is published or updated. Refer to paragraphs 2 & 3 of my reply before this one.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Automatically Change Month and Year in WordPress Titles.’ is closed to new replies.