Get custom posts using the WP REST API - wordpress

I'm trying to get custom post types, but I can not seem to find any solution. The WP REST API documentation only returns blog posts.
Note: I'm using the Dooplay theme
The code below is in the file tipo.php
directory: inc > includes > series
if( ! function_exists( 'doo_series' ) ) {
function doo_series() {
$labels = array(
'name' => _x('TV Shows', 'Post Type General Name','mtms'),
'singular_name' => _x('TV Show', 'Post Type Singular Name','mtms'),
'menu_name' => __d('TV Shows %%PENDING_COUNT_TV%%'),
'name_admin_bar' => __d('TV Shows'),
'all_items' => __d('TV Shows'),
);
$rewrite = array(
'slug' => get_option('dt_tvshows_slug','tvshows'),
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __d('TV Show'),
'description' => __d('TV series manage'),
'labels' => $labels,
'supports' => array('title', 'editor','comments','thumbnail','author'),
'taxonomies' => array('genres'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-view-site',
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
);
register_post_type('tvshows', $args );
}
add_action('init', 'doo_series', 0 );
get_template_part('inc/includes/series/metabox');
}

Compare with
register_post_type( 'post', array(
'labels' => array(
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ),
),
'public' => true,
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_position' => 5,
'hierarchical' => false,
'rewrite' => false,
'query_var' => false,
'delete_with_user' => true,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
'show_in_rest' => true,
'rest_base' => 'posts',
'rest_controller_class' => 'WP_REST_Posts_Controller',
) );
which registers the built in post type post. Observe the last three fields. These fields must be set to make the post type accessible via REST.
ADDENDUM
Actually only 'show_in_rest' is required as WordPress has default values for the other two. Also, if a developer wrote his own WP_REST_Controller for the custom post type instead of using the builtin WP_REST_Posts_Controller then even this is not required as his own WP_REST_Controller can call the register_rest_route() functions by some other means.

Related

Export Wordpress Custom post type Data CSV from Front-end

Please help me. I'm new to WordPress and creating a custom posts. Export WordPress Custom post type Data CSV, XML, and pdf from the Front-end button.
Please help me. I'm new to WordPress and creating a custom posts. Export WordPress Custom post type Data CSV, XML, and pdf from the Front-end button.
here is my current code:-
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type('servicelist',
array(
'labels' => array(
'name' => __( 'Service-Details' ),
'singular_name' => __( 'Service - Detail' ),
'add_new' => 'Add Service',
'all_items' => 'All Service'
),
'description' => __( 'Service Details' ),
'public' => true,
'hierarchical' => true,
'has_archive' => true,
'supports' => array(
'title',
'thumbnail',
'custom-fields',
'page-attributes'
),
'show_in_menu' => true,
'menu_icon' => 'dashicons-visibility',
'menu_position' => 20
));
register_taxonomy('division', 'servicelist',
array(
'hierarchical' => false,
'label' => __('Division'),
'query_var' => 'division',
'rewrite' => array('slug' => 'division' )
));
register_taxonomy('district', 'servicelist',
array(
'hierarchical' => false,
'label' => __('District'),
'query_var' => 'district',
'rewrite' => array('slug' => 'district' )
));
register_taxonomy('upazila', 'servicelist',
array(
'hierarchical' => false,
'label' => __('Upazila'),
'query_var' => 'upazila',
'rewrite' => array('slug' => 'upazila' )
));
register_taxonomy('service-type', 'servicelist',
array(
'hierarchical' => false,
'label' => __('Service type'),
'query_var' => 'service-type',
'rewrite' => array('slug' => 'service-type' )
));
}
<span id="ex" class="ng-binding">Export</span>

How to correct/delete custom post type slug

I have made my own theme. There in function.php i create a custom post-type called Agenda with the slug "schulung":
function ABECrm_post_type() {
$labels = array(
'name' => __( 'Agenden', 'ABECrm' ),
'singular_name' => __( 'Agenda', 'ABECrm' ),
'add_new' => __( 'Neue Agenda', 'ABECrm' )
);
$args = array(
'label' => __( 'agenda', 'ABECrm' ),
'description' => __( 'Hier werden die Agenda-Seiten eingetragen', 'ABECrm' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'custom-fields', 'page-attributes' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'show_in_rest' => true,
'rewrite' => array('slug' => 'schulung'),
'menu_icon' => 'dashicons-admin-page',
'taxonomies' => array( 'post_tag' ),
);
register_post_type( 'agenden', $args );
So first i had the slug "Schulung" - then as mistake "Schulung'" - at the end "schulung".
It seems that there are build redircts now - and the crawler still sees the directory "Schulung" and "Schulung'". At "Schulung" there are "Agendas" made - in "Schulung'" not.
My targets now:
how to delete the information of "Schulung'" and the redirects?
how to delete the directory "Schulung" but not the redirects...

wordpress - how to proper set up archive page?

i have posttypes.php file in which i register post types
<?php
add_action('init', 'lovetocreateelevationprojects_init_posttypes');
function lovetocreateelevationprojects_init_posttypes()
{
$projects_args = array(
'public' => true,
'public_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'
),
'has_archieve' => true
);
register_post_type('projects', $projects_args);
}
I have new post type - gallery and this post type is visible in wordpress administration panel. I created new archive-projects.php file and everything what is inside so far is:
<?php
echo 'its working';
The file seems not to be working because when i enter http://localhost/webname/projects/ path it redirects me to index.php and i dont have 'its working' message.
I use my own link format: http://localhost/webname/%category%/%postname%/
I think every thing is ok but rewrite and query_var correctly to be accessible from url.
<?php
add_action('init', 'lovetocreateelevationprojects_init_posttypes');
function lovetocreateelevationprojects_init_posttypes()
{
$projects_args = array(
'public' => true,
'public_queryable' => true,
'show_ui' => true,
'query_var' => 'projects',
'rewrite' => ['slug'=>'project'],
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array(
'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'
),
'has_archive' => true
);
register_post_type('projects', $projects_args);
}

WP_Post object to string conversion error, when register custom post type

Seen a bunch or responses that talk about post ID, and menus... but this is a brand new blog. I am building a plugin, but when I register a custom post type I get this error...
Catchable fatal error: Object of class WP_Post could not be converted to string in /wp-includes/formatting.php on line 1280
register_post_type('car_post', array(
'labels' => array(
'name' => 'Car Content'
),
'singular_label' => 'Car Content',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'show_in_menu' => 'car-post',
'rewrite' => array("slug" => "car_post"),
'supports' => array( 'title', 'editor', 'author', 'revisions', 'comments' )
)
);
Anyone have an idea on what could possibly be causing that? If I comment out the custom post block... then no errors are thrown.
Custom post types are registered this way.
<?php
add_action( 'init', 'my_post_type' );
function my_post_type() {
register_post_type('car_post', array(
'labels' => array(
'name' => 'Car Content'
),
'singular_label' => 'Car Content',
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'show_in_menu' => 'car-post',
'rewrite' => array("slug" => "car_post"),
'supports' => array( 'title', 'editor', 'author', 'revisions', 'comments' )
)
);
}
?>

Registering custom post type wordpress

I have created new post type in my theme but when i try to get to the permalink for the post, it give me that the post not found
function my_post_type_news() {
register_post_type( 'news',
array(
'label' => __('News'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'rewrite' => array(
'slug' => 'news',
'with_front' => FALSE,
),
'supports' => array(
'title',
'thumbnail',
'editor',
'excerpt')
)
);
register_taxonomy('news_category', 'news', array('hierarchical' => true, 'label' => 'News Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
}
add_action('init', 'my_post_type_news');
function my_post_type_news() {
register_post_type( 'news',
array(
'label' => __('News'),
'public' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'menu_position' => 5,
'rewrite' =>false,
'supports' => array(
'title',
'thumbnail',
'editor',
'excerpt')
)
);
register_taxonomy('news_category', 'news', array('hierarchical' => true, 'label' => 'News Categories', 'singular_name' => 'Category', "rewrite" => true, "query_var" => true));
}
add_action('init', 'my_post_type_news');
Use above function, it will works. I have changed rewrite as false.
Hope it helps.

Resources