Sharing a short snippet I use that checks for meta title and description duplicates from Yoast SEO.
// Add submenu page under Tools
add_action('admin_menu', 'dpmc_add_submenu_page');
function dpmc_add_submenu_page() {
add_submenu_page(
'tools.php',
'Duplicate Product Meta Checker',
'Duplicate Product Meta Checker',
'manage_options',
'dpmc',
'dpmc_submenu_page_callback'
);
}
// Callback function for the submenu page
function dpmc_submenu_page_callback() {
// Check if Yoast SEO is active
if (!class_exists('WPSEO_Meta')) {
echo '<p>Yoast SEO plugin is not active. Please install and activate it.</p>';
return;
}
// Get all active products
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$products = new WP_Query($args);
// Initialize arrays to store product meta data
$product_titles = array();
$product_descriptions = array();
// Loop through products and store meta data
while ($products->have_posts()) {
$products->the_post();
$product_id = get_the_ID();
$product_title = WPSEO_Meta::get_value('title', $product_id);
$product_description = WPSEO_Meta::get_value('metadesc', $product_id);
// Check if title or description is empty
if (empty($product_title) || empty($product_description)) {
continue;
}
// Check if title or description already exists in array
if (in_array($product_title, $product_titles)) {
echo '<p>Duplicate title found in product ID ' . $product_id . ' - ' . get_the_title() . '</p>';
} else {
$product_titles[] = $product_title;
}
if (in_array($product_description, $product_descriptions)) {
echo '<p>Duplicate description found in product ID ' . $product_id . ' - ' . get_the_title() . '</p>';
} else {
$product_descriptions[] = $product_description;
}
}
// Reset post data
wp_reset_postdata();
}