`post_type` => `any` not giving me my custom post
-
24-02-2021 - |
Pregunta
I'm using Roots Bedrock + Sage 9 Beta 3. I've created a custom post of type lp
that I want to set as my homepage. Here is the code I'm using:
function cptui_register_my_cpts_lp() {
/**
* Post Type: Landing Pages.
*/
$labels = array(
"name" => __( 'Landing Pages', 'sage' ),
"singular_name" => __( 'Landing Page', 'sage' ),
);
$args = array(
"label" => __( 'Landing Pages', 'sage' ),
"labels" => $labels,
"description" => "Pages without menus and/or totally custom layouts.",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "lp", "with_front" => true ],
"query_var" => true,
"menu_icon" => "dashicons-welcome-widgets-menus",
"supports" => [ "title", "thumbnail", "excerpt" ],
);
register_post_type( "lp", $args );
}
add_action( 'init', 'cptui_register_my_cpts_lp' );
I've installed mpress-custom-front-page but it doesn't seem to see my landing page.
So, with some digging, I noticed that it uses get_posts()
to get posts with post_type = 'any'
.
The query it sends is so:
$queried_post = get_posts([
'posts_per_page' => - 1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'any',
'post_status' => 'publish',
]);
This query returns all of the posts except my custom post types:
If I redo this exact query, but set post_type
to 'lp'
then I get my post, no problem:
Why would any
not find my custom post type?
Solución
Because when registering your post type you have 'exclude_from_search' => true
get_posts()
is just passing that to WP_Query
. In the parameter definitions for WP_Query
it states:
'any' - retrieves any type except revisions and types with 'exclude_from_search' set to true.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a wordpress.stackexchange