正在尝试在自定义帖子类型中进行搜索;comunicados公司;在自定义插件的短代码中,我将此发送给get\\u posts()
Array
(
[posts_per_page] => -1
[post_type] => comunicados
[post_status] => publish
[orderby] => title
[order] => DESC
[s] => co
)
这里是实际代码
//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);
这将返回所有内容(页面、帖子、其他自定义帖子类型),但;comunicados公司;
我更改为WP\\U查询,结果相同。
如果打印WP\\u查询,我会看到:
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
...
...
...
请注意query和query\\u vars中的post\\u类型。难道不应该是一样的吗?我也看到了这一点:
[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
它没有使用我的post\\u类型,也没有使用这些奇怪的字符串{48AE2FB00693CCB2A14823F0ECE41E120C73BAF24F9E905961BDB4A319D675D}
我不知道该怎么做
这是帖子类型(使用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\' );
奇怪的是,如果;s“;参数不存在,此操作正常:
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
结果查询为:
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