0

Google Search results are showing the date published or date modified of my website pages in the page meta description text. I have root caused it as coming from the Yoast SEO plugin: Schema is outputting "datePublished" and "dateModified" in the schema graph JSON. I have looked at the documentation:

https://developer.yoast.com/features/schema/api/#to-add-or-remove-graph-pieces https://developer.yoast.com/features/schema/plugins/yoast-seo/

but cannot produce a working code snippet to remove these. Either it doesn't work, or it crashes my site.

The code below seems like it should work but broke my site. The documentation has this snippet as an example for removing breadcrumbs in the first function. If there's a way to modify the first function more simply to remove dates I would love to try it.

function remove_breadcrumbs_from_schema( $pieces, $context ) {
    return \array_filter( $pieces, function( $piece ) {
        return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\Breadcrumb;
    } );
}
// functions.php
add_filter( 'wpseo_schema_graph_pieces', 'remove_dates_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_dates_property_from_webpage', 11, 1 );

/**
 * Removes the date graph pieces from the schema collector.
 *
 * @param array  $pieces  The current graph pieces.
 * @param string $context The current context.
 *
 * @return array The remaining graph pieces.
 */

function remove_dates_from_schema( $pieces, $context ) {
    foreach ( $pieces as $key => $piece ) {
        if ( isset( $piece['@type'] ) && in_array( $piece['@type'], ['WebPage', 'Article'], true ) ) {
            if ( isset( $pieces[$key]['datePublished'] ) ) {
                unset( $pieces[$key]['datePublished'] );
            }
            if ( isset( $pieces[$key]['dateModified'] ) ) {
                unset( $pieces[$key]['dateModified'] );
            }
        }
    }
    return $pieces;
}

/**
 * Removes the date property from the WebPage piece.
 *
 * @param array $data The WebPage's properties.
 *
 * @return array The modified WebPage properties.
 */
function remove_dates_property_from_webpage( $data ) {
    if (array_key_exists('date', $data)) {
        unset($data['date']);
    }
    return $data;
}


This seemed promising, but didn't do anything (found it in an article from 3 years ago so maybe it's outdated):

add_filter(
    'wpseo_frontend_presenter_classes'
    , function($filter) {
        return array_diff($filter, [
            'Yoast\WP\SEO\Presenters\Open_Graph\Article_Published_Time_Presenter',
            'Yoast\WP\SEO\Presenters\Open_Graph\Article_Modified_Time_Presenter',
        ]);
    }
);

This worked, but it disabled Yoast Schema output entirely which would be bad for my page's SEO:

add_filter( 'wpseo_json_ld_output', '__return_false' );
1
  • The slash before the array here is probably what's causing the crash in the first snippet. return \array_filter( $pieces, function( $piece ) Apr 2 at 18:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.