$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'); ?>Custom Post Type and API REST is not working|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)

Custom Post Type and API REST is not working

matteradmin10PV0评论

I have created a custom post type called 'customers'. I'm registering step by step as the documentation says. But when I make a call to the rest api, it just returns a 404 response. See the code below:

add_action('init', 'customer_post_type');
function customer_post_type() {
    $labels = array(
        'name'                => _x('Customers', 'customers'),
        'singular_name'       => _x('Customer', 'customers'),
        'menu_name'           => __('Customers', 'customers'),
        'parent_item_colon'   => __('Parent Customer', 'customers'),
        'all_items'           => __('All Customers', 'customers'),
        'view_item'           => __('View Customer', 'customers'),
        'add_new_item'        => __('Add New Customer', 'customers'),
        'add_new'             => __('Add New', 'customers'),
        'edit_item'           => __('Edit Customer', 'customers'),
        'update_item'         => __('Update Customer', 'customers'),
        'search_items'        => __('Search Customer', 'customers'),
        'not_found'           => __('Not Found', 'customers'),
        'not_found_in_trash'  => __('Not found in Trash', 'customers'),
    );

    $args = array(
        'label'               => __('Customers', 'customers'),
        'description'         => __('Customer news and reviews', 'customers'),
        'labels'              => $labels,
        'supports'            => array('title', 'author', 'thumbnail', 'custom-fields', ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'publicly_queryable'  => false,
        'menu_icon'           => 'dashicons-desktop',
        'show_in_rest'        => true,
        'rest_base'           => 'customer',
        'rest_controller_class' => 'WP_REST_Post_Controller'
    );

    register_post_type('customer', $args);
}

the urls that I am testing for, are: /wp-json/wp/v2/customers and /wp-json/wp/v2/customer

I have created a custom post type called 'customers'. I'm registering step by step as the documentation says. But when I make a call to the rest api, it just returns a 404 response. See the code below:

add_action('init', 'customer_post_type');
function customer_post_type() {
    $labels = array(
        'name'                => _x('Customers', 'customers'),
        'singular_name'       => _x('Customer', 'customers'),
        'menu_name'           => __('Customers', 'customers'),
        'parent_item_colon'   => __('Parent Customer', 'customers'),
        'all_items'           => __('All Customers', 'customers'),
        'view_item'           => __('View Customer', 'customers'),
        'add_new_item'        => __('Add New Customer', 'customers'),
        'add_new'             => __('Add New', 'customers'),
        'edit_item'           => __('Edit Customer', 'customers'),
        'update_item'         => __('Update Customer', 'customers'),
        'search_items'        => __('Search Customer', 'customers'),
        'not_found'           => __('Not Found', 'customers'),
        'not_found_in_trash'  => __('Not found in Trash', 'customers'),
    );

    $args = array(
        'label'               => __('Customers', 'customers'),
        'description'         => __('Customer news and reviews', 'customers'),
        'labels'              => $labels,
        'supports'            => array('title', 'author', 'thumbnail', 'custom-fields', ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'publicly_queryable'  => false,
        'menu_icon'           => 'dashicons-desktop',
        'show_in_rest'        => true,
        'rest_base'           => 'customer',
        'rest_controller_class' => 'WP_REST_Post_Controller'
    );

    register_post_type('customer', $args);
}

the urls that I am testing for, are: /wp-json/wp/v2/customers and /wp-json/wp/v2/customer

Share Improve this question edited Jul 26, 2018 at 8:27 junkrig 4583 silver badges11 bronze badges asked Jul 25, 2018 at 20:14 German AlzateGerman Alzate 1211 silver badge4 bronze badges 3
  • This might not fix the full issue, but I would recommend registering the post type (next-to-last line of your code) as singular customer rather than customers plural as the "s" may be throwing something off. You'll probably need to unregister_post_type() first to completely remove the CPT and then re-register with the new name. – WebElaine Commented Jul 25, 2018 at 20:25
  • @WebElaine thanks for advice, but It does not work yet :( – German Alzate Commented Jul 25, 2018 at 20:29
  • the class WP_REST_Post_Controller doesn't exists. I think that it would be better to not define rest_controller_class in your code. – mmm Commented Jul 25, 2018 at 22:00
Add a comment  | 

1 Answer 1

Reset to default -1

Put this code in function.php

add_action( 'init', 'my_customer_cpt' );
  function my_customer_cpt() {
    $labels = array(
        'name'               => _x( 'Customers', 'post type general name', 'your-plugin-textdomain' ),
        'singular_name'      => _x( 'Customer', 'post type singular name', 'your-plugin-textdomain' ),
        'menu_name'          => _x( 'Customers', 'admin menu', 'your-plugin-textdomain' ),
        'name_admin_bar'     => _x( 'Customer', 'add new on admin bar', 'your-plugin-textdomain' ),
        'add_new'            => _x( 'Add New', 'Customer', 'your-plugin-textdomain' ),
        'add_new_item'       => __( 'Add New Customer', 'your-plugin-textdomain' ),
        'new_item'           => __( 'New Customer', 'your-plugin-textdomain' ),
        'edit_item'          => __( 'Edit Customer', 'your-plugin-textdomain' ),
        'view_item'          => __( 'View Customer', 'your-plugin-textdomain' ),
        'all_items'          => __( 'All Customers', 'your-plugin-textdomain' ),
        'search_items'       => __( 'Search Customers', 'your-plugin-textdomain' ),
        'parent_item_colon'  => __( 'Parent Customers:', 'your-plugin-textdomain' ),
        'not_found'          => __( 'No customers found.', 'your-plugin-textdomain' ),
        'not_found_in_trash' => __( 'No customers found in Trash.', 'your-plugin-textdomain' )
    );

    $args = array(
        'labels'             => $labels,
        'description'        => __( 'Description.', 'your-plugin-textdomain' ),
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'customer' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'show_in_rest'       => true,
        'rest_base'          => 'customers-api',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
        'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
    );

    register_post_type( 'customer', $args );
}

Then hit the url http://yourdomian/wp-json/wp/v2/customers-api

If result not showing then simply hit the http://yourdomian/wp-json url then check the routes and search the rest_base name in it 'customers-api'

Make sure you have post in customer post type.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far