-1

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
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 Aug 11 '20 at 12:38
  • @t2pe Edited with the code. – drcrow Aug 11 '20 at 13:35
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.

4
  • Changed the capability_type and nothing changed. Maybe a conflic with something else. Sadly the site is in production so I cant disable all of them to test. – drcrow Aug 13 '20 at 1:06
  • I have a suggestion. I think it seems odd that the output from your WP_Query has several post types except the one you are querying. Its like something is overriding the default nature of WP_Query. If it were a syntax error, I suspect you'd see a more obvious output. So - you could ask a second question on WPSE specifically about how that could occur? This might give you a hint as to where the issue is? Perhaps there is a filter for forcing certain post types to be used in all queries, or perhaps someone has tinkered with WP_Query itself? It might just help you to have more focus on that part? – t2pe Aug 13 '20 at 10:05
  • Effectively, you are right @t2pe. Check my answer up here. Something was overwriting the post_types – drcrow Aug 14 '20 at 13:17
  • Aha, had to be something like that! Glad you found it! – t2pe Aug 14 '20 at 18:25
0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.