Archived topic
Missing Article Schema Markup and published_time for Custom Posts
12 replies · Started by Edmond on February 14, 2021
Hello,
I´ve created my first custom post on my site, and I can see the article schema markup is missing, as well as the date published. I want these custom posts to have the same schema markups as any other regular posts. Could you please help me with this?
FYI, I´ve created these custom posts using the CPT UI plugin.
Thanks!
Edmond
Hi there,
You seem to be using a plugin for this (Yoast).
By default, Yoast doesn't add one for custom post types.
I'm not exactly sure how but I remember them having an article about this in their site. It's best to check their documentation or plugin support on how to do this.
Note: Yoast is a third party plugin outside of our scope of support.
Hi Elvin,
Thanks for your reply.
I found other people with the same problem and managed to fix it. In Yoast´s plugin settings, I went to Search Appearance, then Content Types, and selected Article as a Default Article Type for this custom page.
While this fixed the problem, I still can´t see the published date in the source code (I can see it in the structured data testing tool though), as it considers this page as an ordinary page. For example, my contact page also doesn´t have a published date.
Is there any hooks I can use to fix it too or should I ask Yoast´s support for this too?
Thanks,
Edmond
Hi there,
Are you using any custom functions to alter the post meta output?
I'm seeing two sets of updated dates, instead of the published date and the updated date: https://www.screencast.com/t/fSMIIjjTxmk
Hi Tom,
Yes, I went to inc/structure/post-meta.php and changed this code
$time_string = '<time class="entry-date published" datetime="%1$s"%5$s>%2$s</time>';
to this one:
$time_string = 'Updated on<time class="entry-date updated-date" datetime="%3$s"%6$s>%4$s</time>';
I´ve changed this to show the date modified in the articles.
Thanks
Changing core files like that is never recommended, and in this case, is likely causing the issue.
What happens if you revert the change you've made?
Hi Tom,
Thanks for the recommendation. I´ve reverted the change on the staging area of the site, and while the duplicate gets fixed, but the date published is still missing on Yoast SEO. You can check out the link I´m sending you to see it.
The reason why I made the change in the core and not as you were suggesting it is that Google doesn´t show the modified date in search results.
Thanks,
Edmond
From what I've noticed, Google only likes to display the date when it's the only date on the page.
Try this snippet: https://docs.generatepress.com/article/generate_post_date_show_updated_only/
Hi Tom,
Thanks for this code, this is definitely a safer way to fix this.
Could you also tell me how to make the ¨updated on¨ text be also visible?
Thanks,
Edmond
Hi there,
You can use the generate_post_date_output filter for this.
Example:
add_filter( 'generate_post_date_output', function() {
if( ! is_singular( array('page', 'attachment', 'post') ) ){
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time> ';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="updated" datetime="%3$s" itemprop="dateModified">Updated On %4$s</time> ' . $time_string;
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date('Y') ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date('Y') )
);
return sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
$time_string
)
);
}
});
You can change the date format within get_the_modified_date('Y') and get_the_date('Y') to your preference.
Reference date formats found here: https://www.php.net/manual/en/function.date.php
If you just want to add the "Updated on" text before the date, something like this should work: https://generatepress.com/forums/topic/hi-i-want-to-show-author-updated-date/#post-1645708
Thanks to everyone for the support. This has been resolved.
Glad we could help :)