Created
March 22, 2025 12:59
-
-
Save a1exus/30be3a17ef0b91308843240626a97428 to your computer and use it in GitHub Desktop.
ACF + Jetpack (sitemap)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Dynamic Post Types in Jetpack Sitemap (MU) | |
Description: Dynamically includes all registered public post types (including ACF types) in the Jetpack XML sitemap. | |
Version: 1.4 | |
Author: ChatGPT | |
REF: https://www.advancedcustomfields.com/resources/post-types-and-taxonomies/#registering-custom-post-types | |
REF: https://developer.jetpack.com/hooks/jetpack_sitemap_post_types/ | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
/** | |
* Get all registered public post types for Jetpack sitemap. | |
* | |
* @return array List of valid public post types. | |
*/ | |
function get_dynamic_sitemap_post_types() { | |
static $cached_types = null; | |
if ( is_null( $cached_types ) ) { | |
$excluded_post_types = ['attachment', 'revision', 'nav_menu_item']; | |
$all_post_types = get_post_types(['public' => true], 'names'); | |
$cached_types = array_diff($all_post_types, $excluded_post_types); | |
} | |
return $cached_types; | |
} | |
/** | |
* Add public post types to Jetpack sitemap. | |
* | |
* @param array $post_types Existing post types in the sitemap. | |
* @return array Modified list of post types. | |
*/ | |
function dynamic_jetpack_sitemap_post_types( $post_types ) { | |
return array_unique(array_merge($post_types, get_dynamic_sitemap_post_types())); | |
} | |
/** | |
* Hook into Jetpack sitemap filter once plugins are fully loaded. | |
*/ | |
add_action('plugins_loaded', function() { | |
if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'sitemaps' ) ) { | |
add_filter( 'jetpack_sitemap_post_types', 'dynamic_jetpack_sitemap_post_types' ); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment