1

My custom post type is registered with 'public' as false and I'm querying with a WP_Query() object. arg's 'post_type' set to private too. The url is site.com/?post_type=my_post_type

I've tried to set 'exclude_from_search' attribute to false when registering the , but that didn't get me back the posts. From the docs, it seems that the above url query will be affected. My page is the single-my_custom_post_type.php in the theme folder.

I'm guessing I'll have to create a page with a custom page template in order to have an URL that's not affected by the the front-end search or public queryable attribute.

The question is really is there anyway to keep the default url ?post_type=... and still be able to query the private post type. Thanks.

Answer: Here are the attributes I used:

'public'        => false,
'publicly_queryable' => true,
'exclude_from_search' => true,

Since I want to keep the default URL with post_type query parameter, the publicly_queryable attribute has to be true. To not show to others, I'd have to do conditionals to check whether or not to show the page to the current user.

1

If you are setting the CPT to private, that is telling wordpress not to create the dynamic page for that post type. This includes using the single-CPT.php file.

If you want to have "?post_type=" pages, you'll need to set the cpt to publicly_queryable=true, or public=true (which includes publicly_queryable).

Why do you want it private (aka public = false)?

If you are looking to just have the posts in the back do this:

publicly_queryable=false;  //disables creation of single pages.
has_archive=false;  //disables creation of archive page.
public=true;  //if this is set to false it will hide them in the backed too.

If you keep it this way and would like to show the posts on the front end, they would still be available by using a wp_query. I do this instances where I don't want people to be able to see a single version of my CPTs or if I never have the intention of pulling ALL my posts at the same time. This is also great because they won't get picked up by google or other indexing machines.

  • I'm trying to use the default URL but only show the results for logged in users with their posts. It seems that I'll need to put in the conditional check in the single-...php template itself. – TurtleTread Apr 11 '17 at 15:33
  • yes. that's exactly what you would do. – rudtek Apr 11 '17 at 16:33
  • Your last statement that public=true will hide them in the backend only means that's the default behavior, right? If you query with post_status=private, it will also show up. – TurtleTread Apr 11 '17 at 17:15
  • public=false hides them everywhere, unless you use wp_query or query_posts (although I don't suggest query_posts). does this answer your question? if so please do mark answered. – rudtek Apr 11 '17 at 17:32
  • Yes. If you could please put that in for the answer because my question asks how I can query private posts but keep the default query url. Also I think get_posts is a better function. – TurtleTread Apr 11 '17 at 17:39

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.