0

When I add 'author' I only get results from "post" but no results are received from the "my_post_type_name" My code:

$args = array(
    'author'        =>  $user_id,
    'post_type'      => array('my_post_type_name', 'post'),
);
$wp_query = new WP_Query( $args );

Is it possible? Or do I need two query?

2 Answers 2

0

Ye. It is possible with single query.

WordPress is allowed to fetch posts from different post type. For example, consider the default post types "post" and "page"

To fetch user posts from "post" and "page", we can use the following query.

$wp_query = new WP_Query(array(
    'author' => $user_id,
    'post_type' => array('post', 'page'),
    'posts_per_page' => -1
        )
);

I have used posts_per_page as -1 to fetch all posts. This is working in a default WordPress install.

Refer WP_Query for more details.

1
  • Hey, I changed the name so it would not be confused. I did some tests and indeed when I enter "post" and "page" I get normal results but when I enter "my_post_type_name" I do not get its results. Only when he is alone without a "post" do I get his results
    – yehi co
    Jun 10, 2022 at 10:51
0

problem solved! The code works great, The problem was that the "polylang" plugin filtered out undefined post typs. When I turned off the plugin all the author's posts appeared

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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