I have a page where, with shortcode, I queried multiple posts from a CPT - so the page is one kind of archive page to the general users.

Page Title
  - CPT Post Title #1
  - CPT Post Title #2
  - CPT Post Title #3

In certain condition I want to alter only the Page Title, not CPT Post Title. I tried:

function wpse_change_page_title( $title ) {
   //if( condition matched ) {
       return 'Test ' . $title;
add_filter( 'the_title', 'wpse_change_page_title' );

But it's altering the CPT Post Titles.

Page Title
  - Test CPT Post Title #1
  - Test CPT Post Title #2
  - Test CPT Post Title #3

How can I alter only the Page title, not those CPT Post Titles?

Test Page Title
  • Sorry, this is all inside shortcode output? – s_ha_dum Jun 12 '15 at 16:25
  • No, initially tested there (that filter thing), but then transferred to plugin's index. :) – Mayeenul Islam Jun 12 '15 at 16:56

I believe that the page title you are talking about is the title from the page from the main query. the_title() filter (and the_content() filter for that matter) targets all the respective template tags regardless of query.

To avoid this, target only the main query and the specific page.

You can try the following inside your filter

if ( in_the_loop() && is_page( 'specific page' ) ) {

    // Do what you need to do



I just thought of this, haven't tested it, but you can check your title inside the filter against a known static title and then do something according to that


if ( $title == 'My known title' ) {

    // Do something with the title

  • 1
    That Edit worked. Just need it to be dynamic now. :) Thanks a lot for your time. – Mayeenul Islam Jun 12 '15 at 13:19

I am not entirely sure I understand your project but it sounds to me like you should be using the wp_title filter, instead of the the_title filter. That is fired by the wp_title() function which outputs the page title, not the titles of the posts on the page. Something like:

function wpse_change_page_title( $title ) {
   if(is_page('some_page')) {
       return 'Test ' . $title;
add_filter( 'wp_title', 'wpse_change_page_title' );
  • No, wp_title is not the issue here. The demonstration shows that, everything is in the page content. :) – Mayeenul Islam Jun 12 '15 at 17:53

The second argument for the_title is always a post ID. Use it.

add_filter( 'the_title', function( $title, $post_id) {

    $post = get_post( $post_id );

    if ( ! $post or 'your_cpt' !== $post->post_type )
        return $title;

    return 'Test ' . $title;
}, 10, 2 );

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.