permalink not working on custom post and category - php

I am trying to add wordpress custom post type and category in wordpress
add_action( 'init', 'work_register' );
function work_register() {
$labels = array(
'name' => _x('Work', 'post type general name'),
'singular_name' => _x('Work Item', 'post type singular name'),
'add_new' => _x('Add New', 'work item'),
'add_new_item' => __('Add New Work Item'),
'edit_item' => __('Edit Work Item'),
'new_item' => __('New Work Item'),
'view_item' => __('View Work Item'),
'search_items' => __('Search Work'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => array( 'slug' => 'work', 'with_front'=> false ),
'capability_type' => 'post',
'hierarchical' => true,
'has_archive' => true,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'work' , $args );
register_taxonomy( 'categories', array('work'), array(
'hierarchical' => true,
'label' => 'Categories',
'singular_label' => 'Category',
'rewrite' => array( 'slug' => 'categories', 'with_front'=> false )
)
);
register_taxonomy_for_object_type( 'categories', 'work' );
}
My permalink structure is:
/%category%/%postname%/
When I am adding from post it is giving me link
/category/postname
but when I am adding from custom post, it is giving me only
/postname
I want /category/postname in custom post also
Please suggest where I am wrong

Change your rewrite to add the work post type query var:
'rewrite' => array('slug' => 'work/%categories%')
Then filter post_type_link to insert the selected categories into the permalink:
function custom_work_post_link( $post_link, $id = 0 ){
$post = get_post($id);
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'work' );
if( $terms ){
return str_replace( '%categories%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'custom_work_post_link', 1, 3 );
There are also plugins like Custom Post Type Permalinks that can do this for you.

Related

Custom URL in custom post type, using taxonomy slug

I need a URL like
www.example.com/taxonomy/taxonomy-slug/post-slug
I can get you, but there's something wrong.
I created two items in my taxonomy; ITEM1 and ITEM2
I created a post and I stuck to these two items
On the taxonomy.php page, I opened both pages, that is
www.example.com/taxonomy/item1/
www.example.com/taxonomy/item2/
So far so good, but the post link is coming like this for both pages
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item1/post-slug
And in fact, it should come as
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug
My code below
// Create a post type
function custom_post_type1() {
$labels = array(
'name' => _x( 'Curso', 'Post Type General Name', 'twentythirteen' ),
'singular_name' => _x( 'Curso', 'Post Type Singular Name', 'twentythirteen' ),
'menu_name' => __( 'Curso', 'twentythirteen' ),
'parent_item_colon' => __( 'Curso Relacionado', 'twentythirteen' ),
'all_items' => __( 'Todos os Curso', 'twentythirteen' ),
'view_item' => __( 'Ver Curso', 'twentythirteen' ),
'add_new_item' => __( 'Adicionar novo Curso', 'twentythirteen' ),
'add_new' => __( 'Adicionar novo', 'twentythirteen' ),
'edit_item' => __( 'Editar Curso', 'twentythirteen' ),
'update_item' => __( 'Atualizar Curso', 'twentythirteen' ),
'search_items' => __( 'Buscar por Curso', 'twentythirteen' ),
'not_found' => __( 'Nenhum Curso encontrado', 'twentythirteen' ),
'not_found_in_trash' => __( 'Nenhum Curso encontrado na lixeira', 'twentythirteen' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'curso', 'twentythirteen' ),
'description' => __( 'curso news and reviews', 'twentythirteen' ),
'rewrite' => true,
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'menu_icon' => '',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array( 'slug' => 'curso/%actor%', 'with_front' => false ),
'has_archive' => 'curso'
);
// Registering your Custom Post Type
register_post_type( 'curso', $args );
}
// Create a taxonomy
function add_actor_taxonomy() {
if (!is_taxonomy('actor')) {
register_taxonomy('actor', 'curso' ,
array(
'hierarchical' => FALSE,
'label' => __('actor'),
'public' => TRUE,
'show_ui' => TRUE,
'query_var' => 'actor',
'rewrite' => array( 'slug' => 'unidade', 'with_front' => false )
)
);
}
}
add_action('init', 'add_actor_taxonomy');
//Custom Permalink
function wpa_show_permalinks( $post_link, $post ){
if ( is_object( $post ) && $post->post_type == 'curso' ){
$terms = wp_get_object_terms( $post->ID, 'actor' );
if( $terms ){
return str_replace( '%actor%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'wpa_show_permalinks', 1, 2 );
I hope to return the following
www.example.com/taxonomy/item1/post-slug
www.example.com/taxonomy/item2/post-slug
Can you see something wrong with my code?

Restrict category for custom post type in wordpress

I have created a custom post type for adding employees. I have added the category taxonomy for it. Then I have added a category named "Employees" (slug: employees). But when I add a new employee, it shows me all the categories to choose. How can I restrict it to show only "Employees" category here?
Here is my php code in functions.php:
function employees_register() {
$labels = array(
'name' => _x('Employees', 'post type general name'),
'singular_name' => _x('Employee', 'post type singular name'),
'add_new' => _x('Add New', 'employees'),
'add_new_item' => __('Add New Employee'),
'edit_item' => __('Edit Employee'),
'new_item' => __('New Employee'),
'view_item' => __('View Employee'),
'search_items' => __('Search Employee'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'taxonomies' => array( 'category' ),
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail')
);
register_post_type( 'employees' , $args );
}
add_action('init', 'employees_register');
You should register custom taxonomies with the register_taxonomy() function:
add_action( 'init', 'employee_tax' );
function create_book_tax() {
register_taxonomy(
'employee-categories',
'employees',
array(
'label' => __( 'Employee Categories' ),
'hierarchical' => true,
)
);
}
https://codex.wordpress.org/Function_Reference/register_taxonomy
Then, only these taxonomies will show as options for the post type, rather than Categories of other post types.

Displayin custom taxonomies in custom post type columns

I've looked over several posts about adding custom taxonomies to CPT columns; I'm able to get everything working except actually displaying said taxonomies (publication). Here's my CPT code:
add_action( 'init', 'pb_custom_post_type' );
function pb_custom_post_type() {
$labels = array(
'name' => _x( 'Press', 'post type general name' ),
'singular_name' => _x( 'Press', 'post type singular name' ),
'add_new' => _x( 'Add New', 'review' ),
'add_new_item' => __( 'Add New Press' ),
'edit_item' => __( 'Edit Press' ),
'new_item' => __( 'New Press' ),
'all_items' => __( 'All Press' ),
'view_item' => __( 'View Press' ),
'search_items' => __( 'Search Press' ),
'not_found' => __( 'No press found' ),
'not_found_in_trash' => __( 'No press found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Press'
);
$args = array(
'labels' => $labels,
'description' => 'Press information',
'public' => true,
'menu_position' => 20,
'supports' => array( 'title', 'editor', 'thumbnail' ),
'has_archive' => true,
);
register_post_type( 'press', $args );
}
add_filter( 'manage_edit-press_columns', 'my_edit_press_columns' ) ;
function my_edit_press_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Title' ),
'publication' => __( 'Publication' ),
'date' => __( 'Date' )
);
return $columns;
}
Custom Taxonomy
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
}
Now I've seen two different choices, either trying to add show_ui/show_admin_column in the taxonomy arguments, or having another function with switch statements. I've tried both below, is there something crucial I'm missing?
1
add_action( 'init', 'create_my_taxonomies', 0 );
function create_my_taxonomies() {
$args = array (
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy( 'publication', 'press', array( 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ), $args );
}
2
function custom_columns( $column, $post_id ) {
switch ( $column ) {
case "publication":
echo get_post_meta( $post_id, 'publication', true);
break;
}
}
add_action( "manage_posts_custom_column", "custom_columns", 10, 2 );
Remove your custom columns function, and add 'show_admin_column' => true to the actual array of args for register_taxonomy:
register_taxonomy( 'publication', 'press', array( 'show_admin_column' => true, 'hierarchical' => false, 'label' => 'Publications', 'query_var' => false, 'rewrite' => true ) );
Edit: you might also want to add 'taxonomies' => array( 'publication' ) to the register_post_type args.

Taxonomy link not showing in custom admin menu wordpress

I found many solutions for this but my scenario is total different.
Now my issue is, i am working on a plugin and in plugin i make custom post type and custom taxonomy, firstly its slugs are different, now i want that post type slug and taxonomy slugs are same and i also achieve this successfully but there is a one problem that is my category link is not showing in admin menu because when i register taxonomy i set object_type as slug which is same in post type slug and taxonomy slug but when i change the object type as post type name category menu showing perfectly.
Here is my code
Post Type Code:
add_action('init', 'kbe_articles');
function kbe_articles() {
$labels = array(
'name' => __('Articles', 'kbe'),
'singular_name' => __('Articles', 'kbe'),
'all_items' => __('Articles', 'kbe'),
'add_new' => __('New Article', 'kbe'),
'add_new_item' => __('Add New Article', 'kbe'),
'edit_item' => __('Edit Article', 'kbe'),
'new_item' => __('New Article', 'kbe'),
'view_item' => __('View Articles', 'kbe'),
'search_items' => __('Search Articles', 'kbe'),
'not_found' => __('Nothing found', 'kbe'),
'not_found_in_trash' => __('Nothing found in Trash', 'kbe'),
'parent_item_colon' => ''
);
$kbe_rewrite = array(
'slug' => KBE_PLUGIN_SLUG,
'with_front' => false,
'pages' => true,
'feeds' => true,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => WP_Articles.'images/icon-kbe.png',
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 3,
'supports' => array('title','editor','thumbnail','comments'),
'rewrite' => $kbe_rewrite,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => true
);
register_post_type( 'kbe_articles' , $args );
flush_rewrite_rules();
}
Taxonomy Code:
add_action( 'init', 'kbe_taxonomies');
function kbe_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => __( 'Articles Category', 'kbe'),
'singular_name' => __( 'Articles Category', 'kbe' ),
'search_items' => __( 'Search Articles Category', 'kbe' ),
'all_items' => __( 'All Articles Categories', 'kbe' ),
'parent_item' => __( 'Parent Articles Category', 'kbe' ),
'parent_item_colon' => __( 'Parent Articles Category:', 'kbe' ),
'edit_item' => __( 'Edit Articles Category', 'kbe' ),
'update_item' => __( 'Update Articles Category', 'kbe' ),
'add_new_item' => __( 'Add New Articles Category', 'kbe' ),
'new_item_name' => __( 'New Articles Category Name', 'kbe' ),
);
register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), array(
'hierarchical' => true,
"label" => 'Categories',
"singular_label" => "Articles Category",
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'query_var' => true,
'rewrite' => array( 'slug' => KBE_PLUGIN_SLUG, 'with_front' => true ),
));
flush_rewrite_rules();
}
And this is the code so my post type slug and taxonomy slug are same:
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');
foreach ($post_types as $post_type) {
$post_type_data = get_post_type_object( $post_type );
$post_type_slug = $post_type_data->rewrite['slug'];
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->object_type[0] == $post_type_slug) {
$categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
/* #var $category type */
foreach ($categories as $category) {
$rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );
And here is my menu code:
add_action('admin_menu', 'kbe_plugin_menu');
function kbe_plugin_menu() {
add_submenu_page('edit.php?post_type=kbe_articles', 'Order', 'Order', 'manage_options', 'kbe_order', 'wp_kbe_order');
add_submenu_page('edit.php?post_type=kbe_articles', 'Settings', 'Settings', 'manage_options', 'kbe_options', 'wp_kbe_options');
}
Now when i change register_taxonomy( 'kbe_taxonomy', array('kbe_articles'), My taxonomy link appears in admin custom menu but when i change register_taxonomy( 'kbe_taxonomy', array(KBE_PLUGIN_SLUG), taxonomy link disapears from menu.
So what can i do that my taxonomy link appear in menu. but not change slugs.
Ok here is answer.
I add one more sub menu on a condition
here is my code:
add_submenu_page('edit.php?post_type=foo_articles', 'Categories', 'Categories', 'manage_options', 'edit-tags.php?taxonomy=foo_taxonomy&post_type=foo_articles');

how to get custom post based on custom category in wordpress?

i tried with following code to create custom post and custom category
<?php
/*** added for browse plugin custom post - start ***/
add_action('init', 'demoplugin_register');
function demoplugin_register() {
$labels = array(
'name' => _x('Demoplugin', 'post type general name'),
'singular_name' => _x('Demoplugin Entry', 'post type singular name'),
'add_new' => _x('Add New Demoplugin', 'demoplugin'),
'add_new_item' => __('Add New Demoplugin Entry'),
'edit_item' => __('Edit Demoplugin Entry'),
'new_item' => __('New Demoplugin Entry'),
'view_item' => __('View Demoplugin Entry'),
'search_items' => __('Search Demoplugin Entries'),
'not_found' => __('No Demoplugin Entries found'),
'not_found_in_trash' => __('No Demoplugin Entries found in Trash'),
'parent_item_colon' => ''
);
$slugRule = get_option('category_base');
//if($slugRule == "") $slugRule = 'category';
global $paged;
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'_builtin' => false,
'rewrite' => array('slug'=>'demoplugin','with_front'=>false),
'capability_type' => 'post',
'hierarchical' => false,
'show_in_nav_menus'=> false,
'query_var' => true,
'paged' => $paged,
'menu_position' => 5,
'supports' => array('title','thumbnail','excerpt','editor','comments')
);
register_post_type('demoplugin' , $args);
register_taxonomy("demoplugin_entries",
array("demoplugin"),
array( "hierarchical" => true,
"label" => "Demoplugin Categories",
"singular_label" => "Demoplugin Categories",
'rewrite' => array('slug' => 'demoplugin-category'),
"query_var" => true,
'paged' => $paged
));
flush_rewrite_rules( false );
}
function demoplugin_taxonomies() {
register_taxonomy(
'plugtag',
'demoplugin',
array(
'hierarchical' => false,
'label' => 'Demoplugin Tags',
'query_var' => true,
'rewrite' => array( 'slug' => 'plugtag' ),
)
);
}
add_action('init', 'demoplugin_taxonomies', 0);
add_action('admin_init', 'add_demoplugin');
flush_rewrite_rules(false);
add_action('save_post', 'update_demoplugin');
function add_demoplugin(){
add_meta_box("demoplugin_details", "Demoplugin Options", "demoplugin_options", "demoplugin", "normal", "low");
}
function demoplugin_options(){
global $post;
$custom = get_post_custom($post->ID);
$demoplugin_path_url = $custom["demoplugin_path_url"][0];
$download_url = $custom["download_url"][0];
$demoplugin_video_url = $custom["demoplugin_video_url"][0];
//$demoplugin_excerpt = $custom["demoplugin_excerpt"][0];
$demoplugin_radiogroup = $custom["demoplugin_radiogroup"][0];
if ($demoplugin_radiogroup == '') $demoplugin_radiogroup = 'demoplugin_post_action';
/*** added for browse plugin custom post - stop ***/
?>
above code has taken from functions.php.my requirement is list the custom post based on custom category.
i have tried so many ways to achive my requirement but its fails.
kindly advice on this.
why is there a paged argument? there is none.
query_var => true is invalid it should be false or set as a string, I recommend you skip it at all.
Also there is no singular_label for the taxonomy there is a singular_name for the labels array
Read the docs and validate your code first
http://codex.wordpress.org/Function_Reference/register_post_type
http://codex.wordpress.org/Function_Reference/register_taxonomy

Categories