0

I recently created a custom post type called events where i intend to create posts with artistic events in the my area. How can I access those posts in the site? because if i go to: localhost:####/events it leads me to the index.php, neither to the cpt archive nor to the cpt post-list (with localhost:####/[ctp post-title] i get individual posts).

Also, adding categories is being tricky, when i see the post the category isn't listed.

Also, can i get to those post from the Rest-API? i mean from wp-json/wp/v2/posts

Here the whole functions.php code with taxonomy and the columns setu:

// 1. Custom Post Type Registration (Events)

add_action( 'init', 'create_event_postype' );

function create_event_postype() {

$labels = array(
    'name' => _x('Agenda', 'post type general name'),
    'singular_name' => _x('Agenda', 'post type singular name'),
    'add_new' => _x('Agregar nuevo', 'events'),
    'add_new_item' => __('Agregar nuevo evento'),
    'edit_item' => __('Editar evento'),
    'new_item' => __('Nuevo evento'),
    'view_item' => __('Ver evento'),
    'search_items' => __('Buscar eventos'),
    'not_found' =>  __('No se encuentran eventos'),
    'not_found_in_trash' => __('No hay eventos en la basura'),
    'parent_item_colon' => '',
);

$args = array(
    'label' => __('Agenda'),
    'labels' => $labels,
    'public' => true,
    'can_export' => true,
    'show_ui' => true,
    '_builtin' => false,
    'capability_type' => 'post',
    'menu_icon' => get_bloginfo('template_url').'/images/cal.png',
    'hierarchical' => false,
    'rewrite' => array( "slug" => "events" ),
    'supports'=> array('title', 'thumbnail', 'excerpt', 'editor') ,
    'show_in_nav_menus' => true,
    'taxonomies' => array( 'eventcategory', 'post_tag')
);

register_post_type( 'events', $args);

}

// 2. Custom Taxonomy

function create_eventcategory_taxonomy() {

$labels = array(
    'name' => _x( 'Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Category', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Categories' ),
    'popular_items' => __( 'Popular Categories' ),
    'all_items' => __( 'All Categories' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit Category' ),
    'update_item' => __( 'Update Category' ),
    'add_new_item' => __( 'Add New Category' ),
    'new_item_name' => __( 'New Category Name' ),
    'separate_items_with_commas' => __( 'Separate categories with commas' ),
    'add_or_remove_items' => __( 'Add or remove categories' ),
    'choose_from_most_used' => __( 'Choose from the most used categories' ),
);

register_taxonomy('eventcategory','events', array(
    'label' => __('Event Category'),
    'labels' => $labels,
    'hierarchical' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'event-category' ),
));
}

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

// 3. Show Columns

add_filter ("manage_edit-events_columns", "events_edit_columns");
add_action ("manage_posts_custom_column", "events_custom_columns");

function events_edit_columns($columns) {

$columns = array(
    "cb" => "<input type=\"checkbox\" />",
    "col_ev_cat" => "Categoría",
    "col_ev_date" => "Fecha",
    "col_ev_times" => "Hora",
    "col_ev_thumb" => "Foto",
    "title" => "Evento",
    "col_ev_desc" => "Descripción",
    );
return $columns;
}

function events_custom_columns($column)
{
global $post;
$custom = get_post_custom();
switch ($column)
{
case "col_ev_cat":
    // - show taxonomy terms -
    $eventcats = get_the_terms($post->ID, "eventcategory");
    $eventcats_html = array();
    if ($eventcats) {
    foreach ($eventcats as $eventcat)
    array_push($eventcats_html, $eventcat->name);
    echo implode($eventcats_html, ", ");
    } else {
    _e('None', 'themeforce');;
    }
break;
case "col_ev_date":
    // - show dates -
    $startd = $custom["events_startdate"][0];
    $endd = $custom["events_enddate"][0];
    $startdate = date("F j, Y", $startd);
    $enddate = date("F j, Y", $endd);
    echo $startdate . '<br /><em>' . $enddate . '</em>';
break;
case "col_ev_times":
    // - show times -
    $startt = $custom["events_startdate"][0];
    $endt = $custom["events_enddate"][0];
    $time_format = get_option('time_format');
    $starttime = date($time_format, $startt);
    $endtime = date($time_format, $endt);
    echo $starttime . ' - ' .$endtime;
break;
case "col_ev_thumb":
    // - show thumb -
    $post_image_id = get_post_thumbnail_id(get_the_ID());
    if ($post_image_id) {
    $thumbnail = wp_get_attachment_image_src( $post_image_id, 'post-thumbnail', false);
    if ($thumbnail) (string)$thumbnail = $thumbnail[0];
    echo '<img src="';
    echo bloginfo('template_url');
    echo '/timthumb/timthumb.php?src=';
    echo $thumbnail;
    echo '&h=60&w=60&zc=1" alt="" />';
}
break;
case "col_ev_desc";
    the_excerpt();
break;

}
}
1

In your $args array, add 'show_in_rest' => true,. This will enable the REST API integration.

As for your URLs, you need to flush the rewrite rules. Normally you would want to put your custom post type/taxonomy definitions into a plugin so it is separate from your theme since it deals with content creation/structure as opposed to display. In that you could include a call to flush_rewrite_rules() using the plugin activation/deactivation hooks.

In your case, just go into the permalink settings and save them if you keep it in your theme.

  • Thanks for the 'show_in_rest' property, it worked. 1- Now i can't access the posts per category, say: localhost:7878/wp-json/wp/v2/events?categories=1 That works with posts, not with events (my custom post type) 2- Already flushed permalinks. Can you tell me which would be the url that should post all 'events' posts? 3- Which plugin you recommend? – Marcos Di Paolo Jul 28 '17 at 2:29
  • 1
    You have to call it by the slug of the taxonomy that you registered localhost:7878/wp-json/wp/v2/events?event-category=1 – Cesar Henrique Damascena Jul 28 '17 at 2:37
  • You can set the REST API base URL for your custom post type using the 'rest_base' argument in your $args array. If you want it to be events, then add 'rest_base' => 'events', – Cedon Jul 28 '17 at 2:46

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.