• Hi.

    I have those arguments for my query.

     $args2 = array(
                'post_type' => 'post',
                'post_status' => 'any',
                'cat'   => $category_id,
                'posts_per_page' => -1, // Recupera tutti i post corrispondenti ai criteri
                'author' => $user_id
            );
    

    I log this array and result is this

    [11-Sep-2023 10:22:34 UTC] stampa array args:
    [11-Sep-2023 10:22:34 UTC] post
    [11-Sep-2023 10:22:34 UTC] any
    [11-Sep-2023 10:22:34 UTC] 5841
    [11-Sep-2023 10:22:34 UTC] -1
    [11-Sep-2023 10:22:34 UTC] 17
    

    which looks fine. Than I do this

    $query2 = new WP_Query($args2);
    

    Than I try to log the $query2 with

    error_log($query2->request);
    

    and the result of this log is a query like this

        SELECT   hAvlYjZV_posts.*
        FROM hAvlYjZV_posts  LEFT JOIN hAvlYjZV_term_relationships ON (hAvlYjZV_posts.ID = hAvlYjZV_term_relationships.object_id)
        WHERE 1=1  
        AND (      hAvlYjZV_term_relationships.term_taxonomy_id IN (5841)) 
        AND hAvlYjZV_posts.post_author IN (17)  
    AND hAvlYjZV_posts.post_type IN ('libri', 'ricette', 'cpt_interviste', 'rubriche_originali') 
    AND ((hAvlYjZV_posts.post_status <> 'trash' AND hAvlYjZV_posts.post_status <> 'auto-draft'))
                                GROUP BY hAvlYjZV_posts.ID
                                ORDER BY hAvlYjZV_posts.post_date DESC
    

    Now the problem… post_type seems to filter by ‘libri’, ‘ricette’, ‘cpt_interviste’, ‘rubriche_originali’ (which are custom post types in my implementation) but in my $args2 I explicitly asked for

    'post_type' => 'post',
    

    Why do I have this statement wrong in WP_Query??? Any help here?

    Thanks. Ioria

Viewing 3 replies - 1 through 3 (of 3 total)
  • hi there,

    It’s possible that in the implementation of these custom post types they are always being injected into the query via a filter/action such as pre_get_posts. It’s worth looking at this part of the theme/plugins that are being run, to see if removing that solves the issue.

    Moderator bcworkz

    (@bcworkz)

    Paul is very likely correct about queries being modified. While removing the associated module might work, you will also lose its functionality. Assuming the “pre_get_posts” action is being used to modify queries, you can likely override the modifications using the same action hook. If you add your action callback with a very larger $priority arg, your callback should have the final say in what query var values are used.

    Thread Starter ioria04

    (@ioria04)

    Hi @paulkevan and @bcworkz ,

    I think you are both right. But you know what?

    With this code I solved the problem, I do not know why? (I am not that expert, mine was just a tentative).

    $args2 = array(
                'post_type' => 'post',
                'author__in' => array($user_id),
                'post_status' => 'any',
                'cat'   => $category_id,
                'posts_per_page' => -1,
            );

    Practically i have used

    'author__in' => array($user_id),

    and it worked!!!

    Thanks.

    Mario.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘‘post’ for post_type does not work for WP_Query’ is closed to new replies.