I am trying to get custom posts with WP_Query but it’s not returning only custom post type posts but also default posts too.
I am using
$args = array (
'post_type' => array ('survey')
);
$sPosts = new WP_Query($args);
as a result I am getting ‘survey’ posts as well as default posts, I only need it to return ‘survey’ posts.
P.S. when I use query_posts()
it returns the required result, but just not getting it done with WP_Query and I prefer to use WP_Query
not the query_posts()
Please try it like that….
I believe this custom query should actually be your main query, in which case you should not use a custom query
The problem you are facing is either due to
Wrong use somewhere of
pre_get_posts
. Remember,pre_get_posts
alters all instances ofWP_Query
, backend and front end. Lack of correct use will break all queriesYou have not changed the loop to be objects of your new query
To come back to the point, as said, this is suppose to be the main query, so lets target that problem.
The first thing to do would be to delete the custom query. Once you have done this, you would only see normal post.
We are now going to use
pre_get_posts
to alter the main query to only show custom posts. Paste the following inside your functions.phpYou should now just see post from your cpt on the homepage
EDIT
Your index.php should look something like this
This code can get all the posts in a custom post type..,
wp_query is the function can get all the posts in the custom post type. wp_query requires array, so you can give the custom post type name in post id to array