GET_POSTS和WP_QUERY的奇怪查询

时间:2020-08-11 作者:drcrow

正在尝试在自定义帖子类型中进行搜索;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

2 个回复
最合适的回答,由SO网友:drcrow 整理而成

问题已修复。此脚本介绍函数。php导致了问题

function searchfilter($query) {
    if ($query->is_search && !is_admin() ) {
        $query->set(\'post_type\',array(\'post\',\'page\', \'evento\', \'informes\', \'publicaciones\', \'noticias\', \'comunicados\'));
    }
return $query;
}

add_filter(\'pre_get_posts\',\'searchfilter\');

SO网友:t2pe

我只是尝试复制您的代码,我遇到的唯一问题是CPT设置的一个参数。

你有

"capability_type" => "comunicado",
但这妨碍了CPT对我的正常工作。我认为这个论点不应该是指它本身。我用“post”来代替它,将CPT设置为一个post,然后一切都正常了。在此之前,我无法在仪表板中看到或创建任何Communicados,更改后,我拥有了所有正常的UI。

我不确定这是您的问题的原因,但对于这一更改,我能够很好地运行您的查询代码。

香草Wordpress 5.5。安装了CPT UI,没有其他功能。对功能所做的更改。php和single。php来模拟您的代码。

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post