Network_Query doesn’t query CPT though the Posts are Indexed
Let’s assume a situation that you have a couple websites with custom post types registered:
| Site | Post Types |
|---|---|
| Site 1 | post, page |
| Site 2 | post, product, your_custom_type |
| Site 3 | post |
Everything is indexed well.
But when you are trying a code like this on Site 1 or Site 3 it doesn’t work:
$query = new Network_Query( array( 'post_type' => 'your_custom_type' ) );It is correct. It shouldn’t. Let me explain why.
Network_Query is built on a basis of WP_Query. Both of them uses get_post_types() functions which uses global variables and don’t have any filter hooks inside.
What is the best solution here? If you don’t want a custom post type to be registered on a Site 1 or Site 3? Easy – you can always register CPT without any UI.
add_action( 'init', 'cpt_without_ui' );
function cpt_without_ui() {
register_post_type( 'your_custom_type', array( 'public' => false ) );
}That’s all!
Oh and one more thing in case you are using the same WordPress theme on all sites in your network.
add_action( 'init', 'cpt_without_ui' );
function cpt_without_ui() {
// don't register it again for blog with ID=2
if( 2 == get_current_blog_id() ) {
return;
}
register_post_type( 'your_custom_type', array( 'public' => false ) );
}