Attempting to make a search in a custom post type "comunicados" inside a shortcode in a custom plugin I send this to get_posts()
Array
(
[posts_per_page] => -1
[post_type] => comunicados
[post_status] => publish
[orderby] => title
[order] => DESC
[s] => co
)
Here the actual code
//QUERY
$opt = array(
'posts_per_page' => -1,
'post_type' => 'comunicados',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'DESC'
);
//SEARCH STRING
if($_GET['buscar'] != ''){
$opt['s'] = $_GET['buscar'];
}
print_r($opt);
$res = new WP_Query($opt);
print_r($res);
This returns all the content (pages, posts, other custom post types) but "comunicados"
I changed to WP_Query with the same result.
If I print_r the WP_Query I see this:
WP_Query Object
(
[query] => Array
(
[posts_per_page] => -1
[post_type] => comunicados
[post_status] => publish
[orderby] => title
[order] => DESC
[s] => co
)
[query_vars] => Array
(
[posts_per_page] => -1
[post_type] => Array
(
[0] => post
[1] => page
[2] => evento
[3] => informes
[4] => publicaciones
[5] => noticias
)
[post_status] => publish
[orderby] => title
[order] => DESC
[s] => co
...
...
...
Note the post_type in query and in query_vars. Is not supossed to be the same? Also I see this:
[request] => SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}co{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}') OR (wp_posts.post_excerpt LIKE '{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}co{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}') OR (wp_posts.post_content LIKE '{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}co{48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}'))) AND wp_posts.post_type IN ('post', 'page', 'evento', 'informes', 'publicaciones', 'noticias') AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_title DESC
It's not using my post_type and also these weird strings {48ae2fbb00693ccb2a14823f0ece41e120c73baf24f9e905961bdb4a319d675d}
I'm not sure how to make this work
This is the post type (create with CPT UI)
function cptui_register_my_cpts_comunicados() {
/**
* Post Type: Comunicados.
*/
$labels = [
"name" => __( "Comunicados", "custom-post-type-ui" ),
"singular_name" => __( "Comunicados", "custom-post-type-ui" ),
"menu_name" => __( "Comunicados", "custom-post-type-ui" ),
"all_items" => __( "Todos los comunicados", "custom-post-type-ui" ),
"add_new" => __( "Nuevo comunicado", "custom-post-type-ui" ),
"add_new_item" => __( "Agregar comunicado", "custom-post-type-ui" ),
"edit_item" => __( "Editar comunicado", "custom-post-type-ui" ),
"new_item" => __( "Nuevo comunicado", "custom-post-type-ui" ),
"view_item" => __( "Ver comunicado", "custom-post-type-ui" ),
"view_items" => __( "Ver comunicados", "custom-post-type-ui" ),
];
$args = [
"label" => __( "Comunicados", "custom-post-type-ui" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"delete_with_user" => false,
"exclude_from_search" => false,
"capability_type" => "comunicado",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => [ "slug" => "comunicados", "with_front" => true ],
"query_var" => true,
"supports" => [ "title", "editor", "thumbnail", "excerpt" ],
"taxonomies" => [ "post_tag" ],
];
register_post_type( "comunicados", $args );
}
add_action( 'init', 'cptui_register_my_cpts_comunicados' );
Weird thing is, if the "s" paramether is not present, this works fine:
WP_Query Object
(
[query] => Array
(
[posts_per_page] => -1
[post_type] => comunicados
[post_status] => publish
[orderby] => title
[order] => DESC
)
[query_vars] => Array
(
[posts_per_page] => -1
[post_type] => comunicados
[post_status] => publish
[orderby] => title
[order] => DESC
The resulting query is:
SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'comunicados' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_title DESC