1

I have a plugin that creates a custom post type. However it doesn't create an archive or make the single posts visible and I would like to change that.

This is the code to register the post type:

private static function init_taxonomy() {

    $show_in_menu = current_user_can( 'manage_woocommerce' ) ? 'woocommerce' : $show_in_menu = true;

    register_post_type( 'wc_product_retailer',
        array(
            'labels'                  => array(
                'name'                => __( 'Retailers', 'woocommerce-product-retailers' ),
                'singular_name'       => __( 'Retailer', 'woocommerce-product-retailers' ),
                'menu_name'           => _x( 'Retailers', 'Admin menu name', 'woocommerce-product-retailers' ),
                'add_new'             => __( 'Add Retailer', 'woocommerce-product-retailers' ),
                'add_new_item'        => __( 'Add New Retailer', 'woocommerce-product-retailers' ),
                'edit'                => __( 'Edit', 'woocommerce-product-retailers' ),
                'edit_item'           => __( 'Edit Retailer', 'woocommerce-product-retailers' ),
                'new_item'            => __( 'New Retailer', 'woocommerce-product-retailers' ),
                'view'                => __( 'View Retailers', 'woocommerce-product-retailers' ),
                'view_item'           => __( 'View Retailer', 'woocommerce-product-retailers' ),
                'search_items'        => __( 'Search Retailers', 'woocommerce-product-retailers' ),
                'not_found'           => __( 'No Retailers found', 'woocommerce-product-retailers' ),
                'not_found_in_trash'  => __( 'No Retailers found in trash', 'woocommerce-product-retailers' ),
            ),
            'description'             => __( 'This is where you can add new product retailers that you can add to products.', 'woocommerce-product-retailers' ),
            'public'                  => true,
            'show_ui'                 => true,
            'capability_type'         => 'post',
            'capabilities'            => array(
                'publish_posts'       => 'manage_woocommerce_product_retailers',
                'edit_posts'          => 'manage_woocommerce_product_retailers',
                'edit_others_posts'   => 'manage_woocommerce_product_retailers',
                'delete_posts'        => 'manage_woocommerce_product_retailers',
                'delete_others_posts' => 'manage_woocommerce_product_retailers',
                'read_private_posts'  => 'manage_woocommerce_product_retailers',
                'edit_post'           => 'manage_woocommerce_product_retailers',
                'delete_post'         => 'manage_woocommerce_product_retailers',
                'read_post'           => 'manage_woocommerce_product_retailers',
            ),
            'publicly_queryable'      => true,
            'exclude_from_search'     => true,
            'show_in_menu'            => $show_in_menu,
            'hierarchical'            => false,
            'rewrite'                 => false,
            'query_var'               => false,
            'supports'                => array( 'title' ),
            'show_in_nav_menus'       => false,
        )
    );
}

This is the code I'm using in my functions.php of my child theme.

function change_post_types_slug( $args, $post_type ) {

   /*item post type slug*/   
   if ( 'wc_product_retailer' === $post_type ) {
      $args['has_archive'] = true;
   }

   return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );

It doesn't change anything.

Additionally I've tried directly changing the plugin file by adding

'has_archive'  => true,

below

'show_in_nav_menus'       => false, 

and then even changing

show_in_nav_menus and query_vars to true.

Setting rewrite to 'my-cool-retailer'.

Each of these changes i've gone to peramlinks settings and hit save. I've even disabled the plugin and re-enabled it.

Also, on the post edit screen it continues to not show the post slug under the title.

1

First of all, query_var must be true or a string if you want any sort of front end display to work, whether or not you use pretty permalinks.

The second part is rewrite has to at least be true. Note that valid values for rewrite are either boolean true/false, or an array. This:

$args['rewrite'] = 'my-cool-retailer';

... will enable rewrites, but it won't set the slug to my-cool-retailer, because a string is not a valid value here, it just gets interpreted as true, and your slug will be wc_product_retailer.

If you want to set a different slug, you need to make rewrite an array with key slug:

$args['rewrite']['slug'] = 'my-cool-retailer';

As for has_archive, that's true by default, so that works without changing anything but query_var. You can also pass a string if you want something other than wc_product_retailer to be the archive slug.

So, using your first code block and then setting those two values in the filter works for me when I test on a default install:

function change_post_types_slug( $args, $post_type ) {
    if ( 'wc_product_retailer' === $post_type ) {
        $args['query_var'] = true;
        $args['rewrite']['slug'] = 'my-cool-retailer';
   }
   return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );

One other thing you may want to add is with_front, which is true by default. This will add any static prefix from your post permalink to your CPT, which is usually not desirable.

$args['rewrite']['with_front'] = false;
  • Thanks for the answer. This seemed to work. I added has_archive and made it true as well, but it doesn't make the permalink show up on the post-edit page. Is there a way i can get that back too? – pressword Dec 20 '18 at 16:50
  • What version are you using? Are you using the new or classic editor? – Milo Dec 20 '18 at 16:59
  • wp 5 classic on the custom post – pressword Dec 20 '18 at 17:07
  • I think it may be something with the way the plugin is being coded. When I put the code in by myself it works fine. With this plugin permalinks don't show on the edit screen and when I use the_permalink() in my function doesn't show the postype in the url. it is /the-actual-post instead of /my-cool-retailer/the-actual-post – pressword Dec 20 '18 at 17:34

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.