Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yoast Title in custom post archive-posttype.php is blank when add more query string #16436

Open
1 of 7 tasks
nguyentuandat opened this issue Dec 1, 2020 · 9 comments · May be fixed by #18222
Open
1 of 7 tasks

Yoast Title in custom post archive-posttype.php is blank when add more query string #16436

nguyentuandat opened this issue Dec 1, 2020 · 9 comments · May be fixed by #18222

Comments

@nguyentuandat
Copy link

nguyentuandat commented Dec 1, 2020

  • [x ] I've read and understood the contribution guidelines.
  • [x ] I've searched for any related issues and avoided creating a duplicate issue.

Please give us a description of what happened.

I have

  • archive page: archive-movies.php (post type movies)
  • custom taxonomy: country

When i query in archive-movies.php with url like:
website.com/movies
or
website.com/movies?a=b (with a is not a query_var)
The title replacement work fine like: Movies Archive - Site title

But when i add more query such as:
website.com/movies?country=us (country is my custom taxonomy )
The title just return Movies - Site title (not match SEO title replacement)
I tried to update SEO title replacement but it was not effected ( follow this post ).

I tried add filter:

add_filter( 'wpseo_title', 'add_to_page_titles');
add_filter( 'wpseo_opengraph_title', 'add_to_page_titles' );

function add_to_page_titles( $title ) {
    var_dump($title); die();
}

But just show empty string.

Please describe what you expected to happen and why.

My target the title should be Movies Archive US - Site title.

How can we reproduce this behavior?

  1. Install empty latest Wordpress with theme Twenty Twenty - Version 1.5, setup site permalinks as
    Post name | https://my-web.com/sample-post/
  2. Add custom post type and custom taxonomy
function wporg_custom_post_type() {
    register_post_type('book',
        array(
            'labels'      => array(
                'name'          => __('Books', 'textdomain'),
                'singular_name' => __('Book', 'textdomain'),
            ),
                'public'      => true,
                'has_archive' => true,
        )
    );
}
add_action('init', 'wporg_custom_post_type');

/**
 * Create two taxonomies, genres and writers for the post type "book".
 *
 * @see register_post_type() for registering custom post types.
 */
function wpdocs_create_book_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Genres', 'taxonomy general name', 'textdomain' ),
        'singular_name'     => _x( 'Genre', 'taxonomy singular name', 'textdomain' ),
        'search_items'      => __( 'Search Genres', 'textdomain' ),
        'all_items'         => __( 'All Genres', 'textdomain' ),
        'parent_item'       => __( 'Parent Genre', 'textdomain' ),
        'parent_item_colon' => __( 'Parent Genre:', 'textdomain' ),
        'edit_item'         => __( 'Edit Genre', 'textdomain' ),
        'update_item'       => __( 'Update Genre', 'textdomain' ),
        'add_new_item'      => __( 'Add New Genre', 'textdomain' ),
        'new_item_name'     => __( 'New Genre Name', 'textdomain' ),
        'menu_name'         => __( 'Genre', 'textdomain' ),
    );
 
    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'genre' ),
    );
 
    register_taxonomy( 'genre', array( 'book' ), $args );
 
    unset( $args );
    unset( $labels );
 
    // Add new taxonomy, NOT hierarchical (like tags)
    $labels = array(
        'name'                       => _x( 'Writers', 'taxonomy general name', 'textdomain' ),
        'singular_name'              => _x( 'Writer', 'taxonomy singular name', 'textdomain' ),
        'search_items'               => __( 'Search Writers', 'textdomain' ),
        'popular_items'              => __( 'Popular Writers', 'textdomain' ),
        'all_items'                  => __( 'All Writers', 'textdomain' ),
        'parent_item'                => null,
        'parent_item_colon'          => null,
        'edit_item'                  => __( 'Edit Writer', 'textdomain' ),
        'update_item'                => __( 'Update Writer', 'textdomain' ),
        'add_new_item'               => __( 'Add New Writer', 'textdomain' ),
        'new_item_name'              => __( 'New Writer Name', 'textdomain' ),
        'separate_items_with_commas' => __( 'Separate writers with commas', 'textdomain' ),
        'add_or_remove_items'        => __( 'Add or remove writers', 'textdomain' ),
        'choose_from_most_used'      => __( 'Choose from the most used writers', 'textdomain' ),
        'not_found'                  => __( 'No writers found.', 'textdomain' ),
        'menu_name'                  => __( 'Writers', 'textdomain' ),
    );
 
    $args = array(
        'hierarchical'          => false,
        'labels'                => $labels,
        'show_ui'               => true,
        'show_admin_column'     => true,
        'update_count_callback' => '_update_post_term_count',
        'query_var'             => true,
        'rewrite'               => array( 'slug' => 'writer' ),
    );
 
    register_taxonomy( 'writer', 'book', $args );
}
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'wpdocs_create_book_taxonomies', 0 );
  1. Add new archive-book.php file
<?php
/**
 * The template for displaying archive pages
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since Twenty Seventeen 1.0
 * @version 1.0
 */

get_header(); ?>

<div class="wrap">

	<?php if ( have_posts() ) : ?>
		<header class="page-header">
			<?php
				the_archive_title( '<h1 class="page-title">', '</h1>' );
				the_archive_description( '<div class="taxonomy-description">', '</div>' );
			?>
		</header><!-- .page-header -->
	<?php endif; ?>

	<div id="primary" class="content-area">
		<main id="main" class="site-main" role="main">

		<?php
		if ( have_posts() ) :
			?>
			<?php
			// Start the Loop.
			while ( have_posts() ) :
				the_post();

				/*
				 * Include the Post-Format-specific template for the content.
				 * If you want to override this in a child theme, then include a file
				 * called content-___.php (where ___ is the Post Format name) and that
				 * will be used instead.
				 */
				get_template_part( 'template-parts/post/content', get_post_format() );

			endwhile;

			the_posts_pagination(
				array(
					'prev_text'          => __( 'Previous page', 'twentyseventeen' ),
					'next_text'          => __( 'Next page', 'twentyseventeen' ),
					'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
				)
			);

		else :

			get_template_part( 'template-parts/post/content', 'none' );

		endif;
		?>

		</main><!-- #main -->
	</div><!-- #primary -->
	<?php get_sidebar(); ?>
</div><!-- .wrap -->

<?php
get_footer();
  1. Install Yoast plugin
  2. Run first time wizard
  3. Add new post with post type is book, add genre action
  4. Check Yoast Search apprearance for Book archive:
    %%pt_plural%% Archive %%page%% %%sep%% %%sitename%%
  5. Navigate to url https://my-web.com/book/ --> Site title show correct. Books Archive - Site Name
  6. Navigate to url https://my-web.com/book/?genre=action --> Site title show incorrect. Books - Site Name instead of Books Archive - Site Name

Technical info

Yoast SEO with up to date wordpress 5.5.3

  • If relevant, which editor is affected (or editors):
  • Classic Editor
  • Gutenberg
  • Classic Editor plugin
  • Which browser is affected (or browsers):
  • Chrome
  • Firefox
  • Safari
  • Other

Used versions

  • WordPress version: 5.5.3
  • Yoast SEO version: 15.3
  • Gutenberg plugin version:
  • Classic Editor plugin version:
  • Relevant plugins in case of a bug:
  • Tested with theme: Twenty Twenty - Version 1.5
@Djennez
Copy link
Member

Djennez commented Dec 3, 2020

@nguyentuandat can you share the necessary code / reproduction steps to reproduce your issue from an empty WordPress installation? The current information is not sufficient enough to work with.

@nguyentuandat
Copy link
Author

@nguyentuandat can you share the necessary code / reproduction steps to reproduce your issue from an empty WordPress installation? The current information is not sufficient enough to work with.

Thank @Djennez I just update reproduction steps and code.

@Djennez
Copy link
Member

Djennez commented Dec 8, 2020

Hmmm, I can reproduce the issue, with our Test helper plugin as well.

It looks like adding this argument is making our plugin use the indexable data of the taxonomy that is requested in the argument. So book/?genre=action will make the code think it should return indexable data of the page of the action genre (url/book-genre/action). However, it does then not seem to use the title of that indexable...

@nguyentuandat
Copy link
Author

Thank you!
Are you have any temporary solution for this issue @Djennez ?

@Djennez
Copy link
Member

Djennez commented Mar 15, 2022

A bit of extra investigation;

This goes wrong on

$title     = $this->options->get_title_default( 'title-ptarchive-' . $post_type );

because the indexables think we're on both a posttype archive as well as a taxonomy archive. So it tries to get the default title of title-ptarchive-<taxonomy_name> which does not exist.

The reason the indexables get confused here is because when building the indexable for the current page, we rely on WPQuery, and it looks like WPQuery is doing a query for both the posttype as well as the taxonomy. We have 2 functions where this goes wrong:

	public function for_current_page() {
		$indexable = false;

		switch ( true ) {
			case $this->current_page->is_simple_page():
				$indexable = $this->find_by_id_and_type( $this->current_page->get_simple_page_id(), 'post' );
				break;
			case $this->current_page->is_home_static_page():
				$indexable = $this->find_by_id_and_type( $this->current_page->get_front_page_id(), 'post' );
				break;
			case $this->current_page->is_home_posts_page():
				$indexable = $this->find_for_home_page();
				break;
			case $this->current_page->is_term_archive():
				$indexable = $this->find_by_id_and_type( $this->current_page->get_term_id(), 'term' );
				break;
			case $this->current_page->is_date_archive():
				$indexable = $this->find_for_date_archive();
				break;
			case $this->current_page->is_search_result():
				$indexable = $this->find_for_system_page( 'search-result' );
				break;
			case $this->current_page->is_post_type_archive():
				$indexable = $this->find_for_post_type_archive( $this->current_page->get_queried_post_type() );
				break;
			case $this->current_page->is_author_archive():
				$indexable = $this->find_by_id_and_type( $this->current_page->get_author_id(), 'user' );
				break;
			case $this->current_page->is_404():
				$indexable = $this->find_for_system_page( '404' );
				break;
		}

hits is_term_archive() first, which matches and so it returns term archive data. However:

	public function get_page_type() {
		switch ( true ) {
			case $this->is_search_result():
				return 'Search_Result_Page';
			case $this->is_static_posts_page():
				return 'Static_Posts_Page';
			case $this->is_home_static_page():
				return 'Static_Home_Page';
			case $this->is_home_posts_page():
				return 'Home_Page';
			case $this->is_simple_page():
				return 'Post_Type';
			case $this->is_post_type_archive():
				return 'Post_Type_Archive';
			case $this->is_term_archive():
				return 'Term_Archive';
			case $this->is_author_archive():
				return 'Author_Archive';
			case $this->is_date_archive():
				return 'Date_Archive';
			case $this->is_404():
				return 'Error_Page';
		}

hits is_post_type_archive() first, which matches, so it returns data for a posttype archive. In both cases, when switching the order of the cases, it will hit the one which matches first.

Code that combines these results, like the line at the top of my post, will try to combine these results.

I don't feel like matching the order is the solution here. The code should likely account for WPQueries that return 2 types of results, and determine which one to use.

@NielsdeBlaauw
Copy link
Contributor

NielsdeBlaauw commented Mar 15, 2022

I don't feel like matching the order is the solution here.

I agree it wouldn't be the cleanest solution, but would fix the immediate problem in this case by picking one of the options. Also the behaviour would be somewhat defined and explainable, as opposed to 'random'. Or make it filterable which one to prefer.

The code should likely account for WPQueries that return 2 types of results, and determine which one to use.

I expect it would be hard to create a good UX for this. Either it would have to be determined by Yoast which to pick (which would be the same as above). Or the user would be able to pick for each combination of post-types and terms which SEO title to use. This second solution would get complicated really fast.

@fschroiff
Copy link

Along the lines of Niels' first suggestion: it would help if Yoast picks one of the two options (say "post type archive") as a default and makes the behavior (checking order) customizable via a filter.

@NielsdeBlaauw NielsdeBlaauw linked a pull request Mar 16, 2022 that will close this issue
7 tasks
@akiya64
Copy link

akiya64 commented Mar 16, 2022

Hi, I have same proble when I am improving a WordPress site now.
I tried adding taxonomy template for taxonomy that only use with certain custom type, but YoastSEO evaluate is_post_type_archive() in taxonomy template.
So, title tag is empty at display taxonomy archive and input title tag for each term at term edit page.

According to WordPress template hierarchy and rewrite rule, http:https://examlpe.com/some-post-type/some-term/ call archive-some_post_type.php.
But often I want to let WordPress display taxonomy archive at http:https://examlpe.com/some-post-type/some-term/.

@nguyentuandat
Copy link
Author

For now, change query string (instead of taxonomy name) is a simple solution for me. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants