0

I have created a custom posttype labeled 'Richards' using a site-specific plugin with the following code.

function add_richards_posttype() {
    $labels = array(
        'name'                => _x( 'Richards', 'Post Type General Name' ),
        'singular_name'       => _x( 'Richard', 'Post Type Singular Name' ),
        'menu_name'           => __( 'Richard\'s Dagboek' ),
        'parent_item_colon'   => __( 'Parent Richard' ),
        'all_items'           => __( 'Alle Richards' ),
        'view_item'           => __( 'Bekijk Richards' ),
        'add_new_item'        => __( 'Nieuwe Richard' ),
        'add_new'             => __( 'Nieuwe Richard' ),
        'edit_item'           => __( 'Bewerk Richard' ),
        'update_item'         => __( 'Update Richard' ),
        'search_items'        => __( 'Zoek Richard' ),
        'not_found'           => __( 'Niet Gevonden' ),
        'not_found_in_trash'  => __( 'Niet Gevonden in Trash' ),
    );

    $args = array(
        'label'               => __( 'richards' ),
        'description'         => __( 'Richard\'s Dagboek' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'author', 'thumbnail', 'comments', 'revisions' ),
        // 'taxonomies'          => array( 'kickstarters' ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
    );

    register_post_type( 'richards', $args );

}

add_action( 'init', 'add_richards_posttype', 0 );

I have added data using richards as a posttype, which shows up with the correct posttype in the database. When I want to display either the archive page or single page for this custom post type (by visiting site.com/richards/ or site.com/richard/), WordPress serves a 404 error page.

The reason I have public set to false is to block not-logged-in users from accessing this posttype. Logged-in users should be able to access site.com/richards/ while not-logged-in users should not.

I have searched for the solution for quite a while now and I have tried adjusting permalink settings and changing these back, adding and removing an archive-richards.php file (although I want the styling to fall back on regular posts styling), adding and removing a menu button with ?post_type=richards behind it and some other methods I can't recall.

What else can I try? How would I go about finding the solution now?

5
  • You’ve set public to false... Commented Oct 24, 2017 at 14:51
  • 2
    to show these objects on the front side, you have to set "public" => TRUE,
    – mmm
    Commented Oct 24, 2017 at 14:51
  • I just tried to change it to true and although it now somewhat works, I can also visit these posts without being logged in, which is why I had it set to false. Did I misunderstand the setting description? How would I set them to private again? Also, it now shows up as site.com/news/richards/ instead of site.com/richards/, how would I fix this? Thanks!
    – hans3890
    Commented Oct 24, 2017 at 15:21
  • So, your end goal is to keep this CPT hidden from those not logged in? I think you need to rephrase your initial question to be a little more clear as to what you'd like your final result to look like. Also, go check out the descriptions for public and publicly_queryable in the Codex for register_post_type. I think there's an important distinction to be made there.
    – socki03
    Commented Oct 24, 2017 at 15:28
  • Indeed! The site is mostly used for logged-in users who should be able to see all posts and all custom post types. Not-logged-in users should not be able to see these custom post types. I will rephrase the question and check out the link. Thanks for the information!
    – hans3890
    Commented Oct 24, 2017 at 15:58

3 Answers 3

1

This seems like a similar question, but I'll also put it here.

'has_archive' => 'richards'

Then flush permalinks (Settings > Permalinks).

1
  • I just tried this as it seemed like the solution. Unfortunately it did not change anything. I could be wrong, but wasn't that question more about accessing the custom post type page with a different URL? Thanks for your answer though!
    – hans3890
    Commented Oct 24, 2017 at 15:14
0

Permalinks may need to be refreshed. Once you've saved your code, head to WP-Admin > Settings > Permalinks and click save without changing anything. Then try to visit an archive or single page.

1
  • I had already tried refreshing permalinks but just did it again to be sure. Unfortunately to no avail. Thanks for your answer though!
    – hans3890
    Commented Oct 24, 2017 at 15:22
0

Apparently, I misused the 'public' argument. I was trying to make the entire custom posttype private (which not only should hide all posts, but stop people from knowing about this posttype entirely).

Although I did not yet figure out how to hide a posttype completely, I decided on setting the value back to public (which made everything show up) while setting every post of that custom post type to private. I used another script which forces all publishes for this posttype to become private. I also set 'exclude_from_search' => true to make posts from this custom posttype not show up in search results.

This did create a problem in which the custom post type was used in the URL as a 'child' of the normal posts page (i.e. site.com/news/richards/) though this was fixed using a simple rewrite in the $args:

'rewrite'         => array( 'with_front' => false )

Thanks for all answers!

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.