Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.75k stars 885 forks source link

Removing a CPT from the sitemap but leaving the CPT Archive page in the sitemap does not work #11391

Open rmarcano opened 5 years ago

rmarcano commented 5 years ago

Please give us a description of what happened.

Setting a custom post type not to be indexed will remove it from the sitemap and add a "noindex" to all posts in the CPT.

The Yoast plugin has the option of treating the CPT Archive page separately, by letting you index the archive pages. However, selecting "Yes" in "Show CPT Archive in search results" will not add the CPT Archive to the sitemap, even if the pages do not get a "noindex".

cptarchive

Please describe what you expected to happen and why.

I expected that the CPT Archive page would appear in the sitemap.

How can we reproduce this behavior?

1.Create a Custom Post Type. 2.Go to SEO -> Search Appearance -> Content Types 3.Set the Custom Post Type not to appear in search engines. 4.Set the CPT Archive pages to appear in search engines. 5.Verify that the CPT Archive pages are not in the sitemap.

Technical info

tacoverdo commented 5 years ago

Shouldn't we just hide/remove the settings for the CPTs archive when the CPT is set to not show in search results, @jdevalk?

stodorovic commented 5 years ago

@tacoverdo I'm not sure that's the best approach. It's related to #11225. It's possible that someone want to set all posts to "noindex" by global settings (if there are a lot of posts) and manually set "index" to few posts including archive page. In this case there is inconsistency between sitemap and "indexable" posts (search engines should have not problems finding and indexing them, but they may show warnings).

I'll create PR which will fix it in next weeks. Similar issues (which could be fixed): #11225, #9953, #10312. Maybe it's possible to merge all of them into one issue.

jdevalk commented 5 years ago

No there are valid use cases where you'd want the archive indexed, looking forward to that PR @stodorovic :)

studioavanti commented 5 years ago

Hi, any news about this issue, is it fixed now please?

stodorovic commented 5 years ago

It isn't fixed. I've created couple PRs related to sitemaps in meantime and I didn't find time for this issue. I'll try to create PR soon (it could be my next PR related to sitemaps).

GermanKiwi commented 5 years ago

Just found this issue after experiencing the exact problem myself with a CPT. In my case, I deliberately want to not index the single CPT posts, but I also deliberately do want to index (and include in sitemap) the CPT archive page. Therefore I definitely would not like to see the CPT archive setting hidden, as suggested by tacoverdo above. ;) I'm glad you're working on a fix for this!

ChrisStanyon commented 5 years ago

+1 for this. I need to include a CPT Archive, but exclude all the Single Posts. Had to switch to a different Sitemap generator until this is fixed :(

studioavanti commented 5 years ago

Almost 6 months since this issue has been reported, any news about a fix?

studioavanti commented 5 years ago

Any news please?

Djennez commented 5 years ago

This issue currently has no priority and is not being worked on from within our development team. We do welcome any community patches that would fix this though.

Once this is being picked up, this thread will get notified.

GermanKiwi commented 5 years ago

Hi @Djennez, is it likely that this issue will be picked up at some point in the future? Or are you saying that it's a "won't fix" issue for your team? @stodorovic had mentioned, back in January, that he would try and create a PR for it soon. :) As it's effectively a bug of sorts, and @jdevalk has commented that there are valid use-cases for this scenario, then surely it needs to be fixed/implimented? :)

simonemanfre commented 4 years ago

This bug is incredible, the only way I found to solve the problem is to set each post as noindex from the advanced yoast settings of the single post. Fortunately I have this problem in a post type with 10 posts, but if I had 1000? I would be forced to change the plugin

shikkaba commented 4 years ago

@Djennez It's been a while. Will this issue be fixed anytime in the future?

Djennez commented 4 years ago

I don't believe this has been planned in any of the future projects yet. @JessieHenkes can this possibly be included in the Indexable sitemap overhaul? Or is it too much out of scope for that?

shikkaba commented 4 years ago

@JessieHenkes Any follow up on this?

Djennez commented 4 years ago

As soon as it's worked on / fixed, you'll see this thread being updated.

shikkaba commented 4 years ago

@Djennez Sorry. I don't mean to bother anyone. I know you have lives and such. :) I was just not sure how to pursue it as this issue was opened in 2018, and it would be awesome to know if it was at least on the radar in some way.

acerus commented 3 years ago

@studioavanti @shikkaba @GermanKiwi I've recently stumbled into this bug and thanks to some new filters in Yoast SEO was able to come up with a quick solution that helped me. Sharing it with you (just change $post_type value to whatever post type archive you want to add):

https://wordpressify.ru/2020/12/dobavlenie-svoej-ssylki-v-sitemap-stranits-yoast-seo/

GermanKiwi commented 3 years ago

@acerus thanks so much for providing that function! It works well, and in the absence of an official fix from the developers, this is a really good workaround. 👍

I've actually made a couple of small adjustments to the function, and I'll paste my version of it below. In short: I noticed that your version was outputting the date in the page-sitemap.xml file in this format:

2020-12-20 05:16

Whereas the dates for every other page in this file use the following format:

2020-12-20T04:16:44+00:00

So in order to get this format, I've tweaked your function to look like this:

function add_archive_URL() {
    $post_type = 'my_custom_post_type';
    $archive_url = get_post_type_archive_link( $post_type );

    $args = [
        'posts_per_page'         => 1,
        'post_type'              => $post_type,
        'orderby'                => 'modified',
        'order'                  => 'DESC',
        'no_found_rows'          => true,
        'update_post_meta_cache' => false,
        'update_post_term_cache' => false,
        'fields'                 => 'ids',
        'cache_results'          => true
    ];

    $latest = new WP_Query( $args );

    while ( $latest->have_posts() ) {
        $latest->the_post();
        $date = get_post_modified_time( 'Y-m-d h:i:s', true );
        $last_mod = YoastSEO()->helpers->date->format( $date );
    }

    $url = "\t<url>\n";
    $url .= "\t\t<loc>$archive_url</loc>\n";
    $url .= "\t\t<lastmod>$last_mod</lastmod>\n";
    $url .= "\t</url>\n";

    return $url;

}
add_filter( 'wpseo_sitemap_page_content', 'add_archive_URL' );

Key things I changed:

Hope that's helpful!

acerus commented 3 years ago

@acerus thanks so much for providing that function! It works well, and in the absence of an official fix from the developers, this is a really good workaround. 👍

I've actually made a couple of small adjustments

Thank you, those are actually very helpful adjustments!

MaxDarklighter commented 3 years ago

@acerus , @GermanKiwi what if I need to add several CPT archive pages to my sitemap.xml - do I need to copy/paste our code several times or may be I can just use commas, separating my post_type titles?

GermanKiwi commented 3 years ago

@MaxDarklighter that's a good question. My suspicion is that you may need to make additional copies of the function - one per CPT, with each function having a unique name of course. However, it's entirely possible I'm wrong, and hopefully someone with deeper knowledge of functions could give a more authoritative answer here.

At the very least, it will certainly work fine to use multiple functions. It won't break anything. I just don't know if it's the most efficient way to do it.

MaxDarklighter commented 3 years ago

@GermanKiwi just 10min ago I tried to do it and I got critical error on my site. But no worries - I deleted all copies of "function add_archive_URL()" and it's ok now :)

GermanKiwi commented 3 years ago

@MaxDarklighter if you're adding more than one copy of the function, it's important that each copy has a unique name, which is mentioned in both the 1st line (beginning with "function") and last line (beginning with "add_filter").

So for example:

function add_archive_URL_CPT1() {
    $post_type = 'my_custom_post_type_1';

    (REST OF THE FUNCTION)
}
add_filter( 'wpseo_sitemap_page_content', 'add_archive_URL_CPT1' );

function add_archive_URL_CPT2() {
    $post_type = 'my_custom_post_type_2';

    (REST OF THE FUNCTION)
}
add_filter( 'wpseo_sitemap_page_content', 'add_archive_URL_CPT2' );

function add_archive_URL_CPT3() {
    $post_type = 'my_custom_post_type_3';

    (REST OF THE FUNCTION)
}
add_filter( 'wpseo_sitemap_page_content', 'add_archive_URL_CPT3' );
Al5ki commented 3 years ago

Creating multiple functions didn't' work for me. It only took notice of the last one (despite varying IDs).

In the end I created Pages with the same Slug as the Archive Pages and they appeared in the index but organically redirected to the correct page.

joelnewcomer commented 2 years ago

I needed a solution for multiple languages in WPML so I've modified this code for that and am sharing in case anyone else needs it (I'm not sure why the insert code feature isn't wanting to work for me):

` function add_archive_URL() { $post_type = 'my_custom_post_type'; $url = '';

$active_languages = apply_filters( 'wpml_active_languages', false );
$current_language = apply_filters( 'wpml_current_language', NULL );

if ( $active_languages ) {
    foreach ( $active_languages as $code => $data ) {
        do_action( 'wpml_switch_language', $code ); 

        $archive_url = get_post_type_archive_link( $post_type );

        $args = [
            'posts_per_page'         => 1,
            'post_type'              => $post_type,
            'orderby'                => 'modified',
            'order'                  => 'DESC',
            'no_found_rows'          => true,
            'update_post_meta_cache' => false,
            'update_post_term_cache' => false,
            'fields'                 => 'ids',
            'cache_results'          => true
        ];

        $latest = new WP_Query( $args );

        while ( $latest->have_posts() ) {
            $latest->the_post();
            $date = get_post_modified_time( 'Y-m-d h:i:s', true );
            $last_mod = YoastSEO()->helpers->date->format( $date );
        }

        $url .= "\t<url>\n";
        $url .= "\t\t<loc>$archive_url</loc>\n";
        $url .= "\t\t<lastmod>$last_mod</lastmod>\n";
        $url .= "\t</url>\n";

    }
    do_action( 'wpml_switch_language', $current_language );
}   
return $url;

} add_filter( 'wpseo_sitemap_page_content', 'add_archive_URL' );`

warudin commented 2 years ago

"Show [CPT-name] in search results?" -> Off "Show the archive for CPT-name in search results?" -> On

With the above settings, it would be expected to see the archive page in the sitemap. This is currently not the case.

I can see the 'robots' meta tag in the head of the archive page change from 'index' to 'noindex' when changing the second setting, fortunately.

jordan-webdev commented 3 months ago

The code by @GermanKiwi wasn't working for me. The following does. Perhaps this will help someone in 2024. Note, I did mine for a CPT called project, you'll need to change it for your needs.

Basically, all I'm doing is filtering into the WHERE clause in the SQL call the plugin makes and making it so that it returns no results. That way, it will only show the archive page and no single posts in the sitemap.

`<?php // For projects, hide the single projects in the sitemap (show only the archive) add_filter( 'wpseo_posts_where', function( $where, $post_type ) { // Check if this is the post type you want to modify if ( $post_type === 'project' ) { // Add a condition that always evaluates to false $where .= " AND 1 = 0 "; }

return $where;

}, 10, 2 ); `