• Resolved Pioneer Web Design

    (@swansonphotos)


    I am using the following for our news CPT. The site was redone recently and was using no SEO, sitemaps, and redirects to achieve the, for example, desired URL structure:

    /news/2017/09/08/article-name/

    So I set up the CPT and below:

    Sitewide permalinks are:

    /%year%/%monthnum%/%day%/%postname%/

    //CPT above
    
    register_post_type( 'qa_news', $args );
    
    // Add the permalink structure we want:
    	add_permastruct(
    		'qa_news',
    		'/news/%year%/%monthnum%/%day%/%name%/',
    		false
    	);
    	// Matching nice URLs to the URLs WP will use to get news(s)...
    	// All news from one day:
    	add_rewrite_rule(
    		'^news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})(/page/([0-9]+))?/?$',
    		'index.php?post_type=qa_news&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[5]',
    		'top'
    	);
    	// An individual news article:
    	add_rewrite_rule(
    		'^news/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/([^/]+)/?$',
    		'index.php?post_type=qa_news&name=$matches[4]',
    		'top'
    	);
    	// All news from one month:
    	add_rewrite_rule(
    		'^news/([0-9]{4})/([0-9]{1,2})(/page/([0-9]+))?/?$',
    		'index.php?post_type=qa_news&year=$matches[1]&monthnum=$matches[2]&paged=$matches[4]',
    		'top'
    	);
    	// All news from one year:
    	add_rewrite_rule(
    		'^news/([0-9]{4})(/page/([0-9]+))?/?$',
    		'index.php?post_type=qa_news&year=$matches[1]&paged=$matches[3]',
    		'top'
    	);
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'news_post_type', 0 );
    
    /**
     * Have our custom post type, News, use the nice permalinks we wanted.
     * Should tie in with the add_permastruct() we used when creating the post
     * type.
     */
    function qa_news_permalinks( $url, $post ) {
        if ( 'qa_news' == get_post_type( $post ) ) {
            $url = str_replace( "%year%", get_the_date('Y'), $url );
            $url = str_replace( "%monthnum%", get_the_date('m'), $url );
            $url = str_replace( "%day%", get_the_date('d'), $url );
            $url = str_replace( "%name%", $post->post_name, $url );
        }
        return $url;
    }
    add_filter( 'post_type_link', 'qa_news_permalinks', 10, 2 );

    It is definitely working, The only issue is we are getting a whacked News CPT sitemap index.

    example: site.com/news////article-name/

    (the year, month, date text are missing!)

    And the large search engine is complaining as it should since we just submitted the sitemap last week and these URL’s have been added to the index..

    This most likely related to Yoast (non-Pro version) topic has been open for a very long time:

    https://www.ads-software.com/support/topic/custom-post-type-slug-rewrite-not-recognized-by-yoast/

    I have found no resolution and it does not look like Yoast can/will help in their support forum here?

    Anyone come across this and have a resolution that is tested and working today? Any thoughts?

    Thanks in advance!

    The page I need help with: [log in to see the link]

Viewing 10 replies - 1 through 10 (of 10 total)
  • There are other sitemap generators out here, too.

    I’d try a few of them to see if there’s a problem with sitemaps in general, the permalinks, or just the one plugin.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    @jnashhawkins ,

    Thanks for your input.

    I’m looking again only for, if I may please:

    Anyone come across this and have a resolution that is tested and working today?

    Again, thanks.

    Well, I’d recommend drilling into the issue with another plugin. You’ll need to tell the Yoast plugin to ‘not create sitemaps’. Else you’ll get a conflict with your new sitemapper plugin and mixed or broken results. There’s a setting in Yoast for that.

    I’ve never used custom post types except where plugins created them but I always thought the built-in Pretty Permalink mechanism in WordPress was capable of dealing with those and have never created custom permalinks for one post type. I’ve never even considered that idea but that might be another area to explore.

    You might also ask for help with your code snippet over at someplace such as Stack Exchange. Custom code is outside the experience level of many of the support people here.

    Here is one thread from Stack Exchange that might be useful to you. https://wordpress.stackexchange.com/questions/128408/improve-custom-post-permalink-structure

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    @jnashhawkins ,

    Custom code is outside the experience level of many of the support people here.

    Clearly.

    Please help others help me and read again my post and see my experience level. Your input is not helpful.

    Moderator bcworkz

    (@bcworkz)

    Hi PWD — you’ve been around here for a long time, so I’m sure you know that getting working tested code here is a long shot. Worth a try, but a long shot.

    Please note you did also ask for “Any thoughts?” so you’ve left yourself open to replies that may not be exactly what you are looking for. Along the same line, I think a viable approach is to exclude your CPT from sitemap listing in Yoast’s sitemap settings. Then append your correct CPT URLs to the Yoast sitemap output that are generated by your own code. There is some way via hooks and object methods to register custom sitemaps in Yoast that it will add to its main index and call the supplied callback to generate the index linked sitemap when requested. I don’t know the specifics, but I know it can be done.

    The sitemaps are cached so you will also need to flush the current cache before your additions show up. Relevant code is in the plugin’s /inc/sitemaps/ folder. I know this is not what you asked for, but it’s a “thought” giving you a viable direction towards a solution.

    While it’s now out dated, this code is the general approach I’m speaking of:
    https://gist.github.com/mohandere/4286103ce313d0cd6549
    It could probably be brought up to date without too much fuss.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    @bcworkz

    Sure, I know, which is why I got annoyed when folks who could not actually offer a solution posted.

    And, I used to have no problem getting that kind of support here. That was before the talent went commercial?

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    @swansonphotos I’ve removed your other topic. Please do not make a topic about a topic.

    Thread Starter Pioneer Web Design

    (@swansonphotos)

    @jdembowski,

    Sure, ty for helping.

    I’ve seen that you have created an issue on Yoast SEO, but it isn’t issue in Yoast SEO because it uses function get_permalink for sitemap URLs.

    Issue is in your function qa_news_permalinks because the code assumes that $post passed as second argument and global $post are same object. It could make a lot of side effects including sitemap’s issue.

    First of all, you should improve the permalink structure (use %qa_news% instead of %name%):

    // Add the permalink structure we want:
    add_permastruct(
        'qa_news',
        '/news/%year%/%monthnum%/%day%/%qa_news%/',
        false
    );
    

    Second issue is function get_the_date. You could pass $post object as second argument or use following optimized code:

    /**
     * Have our custom post type, News, use the nice permalinks we wanted.
     * Should tie in with the add_permastruct() we used when creating the post
     * type.
     */
    function qa_news_permalinks( $url, $post ) {
            // Abort if post is not a news post.
            if ( 'qa_news' !== $post->post_type ) {
                   return $url;
            }
    
            $post_date = strtotime( $post->post_date );
    
            $find = array(
                    '%year%',
                    '%monthnum%',
                    '%day%',
            );
    
            $replace = array(
                    date( 'Y', $post_date ),
                    date( 'm', $post_date ),
                    date( 'd', $post_date ),
            );
    
            return str_replace( $find, $replace, $url );
    }
    
    add_filter( 'post_type_link', 'qa_news_permalinks', 10, 2 );
    
    Thread Starter Pioneer Web Design

    (@swansonphotos)

    @stodorovic ,

    I have applied the changes above, reset the permalinks, turned Yoast sitemaps off, then back on – and the issue is resolved.

    Thank you very much for this detailed resolution to this issue.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Sitemap CPT URL’s missing date text – year month day’ is closed to new replies.