You can somehow get the full post title on the ID given that there can be seo plugins that change the title.

That is, I need what is displayed in <title>...</title> on single posts or on pages. For example, the main page displays "Site Title - Tagline". On pages - "Page Title - Site Title"

So I need an end result to take into account all the hooks \ filters that change the final format of the title

Added by:

 if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) { $wp_query->the_post(); $id = get_the_id(); $routes[ $id ] = array( ... 'title' => get_wp_title( $id ) ); } } wp_reset_postdata(); 

in the code cut out all unnecessary but not to be confused

  • All SEO plugins can not be taken into account, because they can contain absolutely any errors. - KAGG Design

1 answer 1

The full title of the post with all filters in the plugins returns wp_document_title() .

The problem is that this function only works with the current page. The following code "tricks" this function by saving the current wp_query query, making a new main (this is important!) WordPress query using query_posts() , resetting the query result using wp_reset_query() and restoring the old query in the global wp_query variable.

 function get_wp_title( $id ) { global $wp_query; $old_wp_query = null; $old_wp_query = $wp_query; $args = array( 'post_type' => 'any', 'post_status' => 'publish', 'p' => $id, ); query_posts( $args ); if ( class_exists( WPSEO_Frontend ) ) { WPSEO_Frontend::get_instance()->reset(); } $title = wp_get_document_title(); wp_reset_query(); if ( ! empty( $old_wp_query ) ) { $GLOBALS['wp_query'] = $old_wp_query; unset( $old_wp_query ); } return $title; } 

Using:

 echo get_wp_title( $post_id ); 

Code tested, works.

UPDATE

When using the Yoast SEO plugin, the situation becomes much more complicated. This plugin itself accesses the internal global variables of WordPress and caches the page title in the private member of its class. In order to bypass the caching of Yoast SEO, the following lines were added to the code:

 if ( class_exists( WPSEO_Frontend ) ) { WPSEO_Frontend::get_instance()->reset(); } 

It is impossible to make any common code for any SEO plugin , as stated in the question. Only for Yoast had to resort to in-depth analysis of its code with a debugger to add just one function call.

UPDATE

Since there were questions about the health of the code, I provide step-by-step instructions for creating a test case.

  1. A test page so733846 has been created on the site test.kagg.eu
  2. The page-so733846.php file has been created in the theme folder.

The following code is placed in the file.

 <?php // 733846 function get_wp_title( $id ) { global $wp_query; $old_wp_query = null; $old_wp_query = $wp_query; $args = array( 'post_type' => 'any', 'post_status' => 'publish', 'p' => $id, ); query_posts( $args ); if ( class_exists( WPSEO_Frontend ) ) { WPSEO_Frontend::get_instance()->reset(); } $title = wp_get_document_title(); wp_reset_query(); if ( ! empty( $old_wp_query ) ) { $GLOBALS['wp_query'] = $old_wp_query; unset( $old_wp_query ); } return $title; } function so_733846() { $wp_query = new WP_Query( [ 'post_type' => 'any', 'post_status' => 'publish', 'posts_per_page' => - 1, ] ); if ( $wp_query->have_posts() ) { while ( $wp_query->have_posts() ) { $wp_query->the_post(); $id = get_the_id(); echo get_wp_title( $id ) . '<br>'; } } wp_reset_postdata(); } so_733846(); 

The result of the work can be seen here: http://test.kagg.eu/so733846/

He is such a:

enter image description here

  • Yoast SEO 5.6.1 is, if you cut it down then everything is ok. But I was looking for a solution to the problem given that we do not know what the seo plugin will be - Pavel
  • It is IMPOSSIBLE to answer the question for any plugins. I had to climb a Yoast SEO code with the phpStorm + xDebug debugger for a couple of hours to figure out how it works and find just one call to the function that flushes the cache. See the updated answer. - KAGG Design
  • And then - what kind of task is this you can plug into the site, and what kind of plug-in is it worth - you don't know - KAGG Design
  • I just thought that there is a solution that gives the final version of the title - Pavel
  • Comments are not intended for extended discussion; conversation moved to chat . - Qwertiy