最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

get posts - Weird query with get_posts and WP_Query

programmeradmin1浏览0评论

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

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
Share Improve this question edited Aug 13, 2020 at 1:28 drcrow asked Aug 11, 2020 at 3:14 drcrowdrcrow 1717 bronze badges 2
  • It might be helpful to add the exact code you are running for get_posts rather than just the argument Array output. So for example if you are constructing the arguments into a variable first and then passing them like get_posts($args), edit your post to include both. The reason is there could be a format or syntax issue in the code you are using? – t2pe Commented Aug 11, 2020 at 12:38
  • @t2pe Edited with the code. – drcrow Commented Aug 11, 2020 at 13:35
Add a comment  | 

2 Answers 2

Reset to default 0

I just tried to replicate your code and the only issue I came up with is in one of the arguments to your CPT setup.

You have

"capability_type" => "comunicado",

But this prevented the CPT working correctly for me. I don't think this argument should be referring to itself. I replaced it with 'post' to set the CPT to act like a Post and things then worked ok for me. Before this I could not see or create any Communicados in the Dashboard - after I changed it I had all the normal UI.

Not sure that this is the cause of your issue but for that one change I was able to run your Query code just fine.

Vanilla Wordpress 5.5. with CPT UI installed and nothing else. Changes made to functions.php and single.php in order to simulate your code.

Problem fixed. This script on functions.php was causing the problem

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');
发布评论

评论列表(0)

  1. 暂无评论