I adapted an existing solution to add a prefix to expired posts titles. It works, but it adds the prefix also to the main menu elements and to the Previous/Next links (that are not expired yet). I removed the prefix from the menu elements by adding a supplementary in_the_loop() to the if condition, but this doesn't removed the prefix from the Previous/Next links. How to solve this?

function wpd_title_prefix_filter( $title, $post_id ) {
    if( in_the_loop() && is_singular( 'facebook_events' ) && 'expired' == get_post_status() ){
        $title = '<span class="expired-event">[expirat]</span> ' . $title;
    return $title;
add_filter( 'the_title', 'wpd_title_prefix_filter', 10, 2 );


I solved it with CSS, but why this happens?

.nav-title .expired-event {
    display: none;

1 Answer 1


This happens because the_title filter is run when the the_title() or related function is called to print the title, and prev/next links functions run the same the_title filter.

But, the problem here is that you are having general conditions that main post and prev/next fulfil: singular, expired post status and loop. To target the main post only in the single template add condition $post_id == get_the_ID() to your IF - $post_id is the filter function argument, and get_the_ID() get's the ID of the global $post.

  • Only the main post is expired, not prev/next links.
    – Yurié
    Oct 28, 2017 at 15:54
  • Yes, I get that. But, your conditions don't deal with the $post_id sent by the the_title filter, they are for global $post ( the main post loaded), and the_title is run for prev/next too, and same conditions are again true because they still check global $post. That is why you need to check the ID of the post to make sure you are targeting the global $post. Oct 28, 2017 at 16:00
  • I replaced the in_the_loop() with $post_id == get_the_ID() and now is OK. Thank you very much!
    – Yurié
    Oct 28, 2017 at 16:04

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.