$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>how to alter args of a custom post type|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

how to alter args of a custom post type

matteradmin9PV0评论

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.

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.

Share Improve this question asked Dec 19, 2018 at 23:04 presswordpressword 3801 gold badge4 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far