I have a custom post type and custom taxonomy set up using the code below in functions.php.
The URL writes correctly when clicking through on the archive pages on the front end and when clicking "View Post" in the admin edit screen. But when the post is returned in site search results, the custom taxonomy is literally missing. The URL on search results is http://www.example.com/foo//postname.
Any idea why the custom taxonomy in the URL would work in some situations but not in others?
add_action( 'init', 'create_foo_posttype' );
function create_foo_posttype() {
$tax_labels = array(
'name' => _x( 'Foo Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Foo Category', 'taxonomy singular name' ),
'all_items' => __( 'All Foo Categories' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Foo Category' ),
'update_item' => __( 'Update Foo Category' ),
'add_new_item' => __( 'Add New Foo Category' ),
'new_item_name' => __( 'New Foo Category Name' ),
);
register_taxonomy('foo_categories','foo',array(
'labels' => $tax_labels,
'hierarchical' => true,
'has_archive' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'foo' )
));
register_post_type( 'foo',
array(
'labels' => array(
'name' => __( 'Foo' ),
'singular_name' => __( 'Foo' )
),
'public' => true,
'exclude_from_search' => false,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 100,
'capability_type' => 'post',
'supports' => array( 'title', 'author', 'thumbnail', 'trackbacks', 'revisions' ),
'taxonomies' => array( 'foo_categories' ),
'has_archive' => true,
'rewrite' => array( 'slug' => 'foo/%foo_categories%')
)
);
flush_rewrite_rules();
}
add_action( 'init', 'cust_rewrite_init' );
function cust_rewrite_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'cust_rewrite_collect_page_rewrite_rules' );
function cust_rewrite_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['cust_rewrite_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'cust_rewrite_prepend_page_rewrite_rules' );
function cust_rewrite_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['cust_rewrite_page_rewrite_rules'] + $rewrite_rules;
}
I found the problem. It wasn't in the code I posted originally. It was here:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$post_link = str_replace( '%foo_categories%', $post_type_term[0]->slug, $post_link );
}
return $post_link;
}
Using $post_type_term[0]->slug; worked if a post had more than one tax value, but failed when the post had only one (which all of my posts do). To fix it, I used array_pop instead of the other array call and it works fine. The resolved code:
add_filter('post_type_link', 'cust_permalink_structure', 1, 4);
function cust_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%foo_categories%' ) ) {
$post_type_term = get_the_terms( $post->ID, 'foo_categories' );
$one_term = array_pop($post_type_term);
$post_link = str_replace( '%foo_categories%', $one_term->slug, $post_link );
}
return $post_link;
}
Related
How can I add the string "p" in front of every post url in this cutom post type?
I added it in my code, wordpress is generating the link but when I click, it returns 404.
What do I need to do?
<?php
/**
* news ====================================================
*/
add_action('init', 'my_custom_news');
function my_custom_news()
{
$labels = array(
'name' => _x('新着情報', 'post type general name'),
'singular_name' => _x('news', 'post type singular name'),
'add_new' => _x('新しく新着情報を書く', 'news'),
'add_new_item' => __('新着情報記事を書く'),
'edit_item' => __('新着情報記事を編集'),
'new_item' => __('新しい新着情報記事'),
'view_item' => __('新着情報記事を見る'),
'search_staff' => __('新着情報記事を探す'),
'not_found' => __('新着情報記事はありません'),
'not_found_in_trash' => __('ゴミ箱に新着情報記事はありません'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array(
"slug" => "news",
"with_front" => false
),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'supports' => array('title','excerpt','editor','revisions'),
'has_archive' => true
);
register_post_type('news',$args);
}
/** ---- 新着情報タクソノミー追加 ---- **/
add_action ('init','create_newscat_taxonomy','0');
function create_newscat_taxonomy () {
$taxonomylabels = array(
'name' => _x('新着情報カテゴリー','post type general name'),
'singular_name' => _x('新着情報カテゴリー','post type singular name'),
'search_items' => __('新着情報カテゴリー'),
'all_items' => __('新着情報カテゴリー'),
'parent_item' => __( 'Parent Cat' ),
'parent_item_colon' => __( 'Parent Cat:' ),
'edit_item' => __('新着情報カテゴリーを編集'),
'add_new_item' => __('新着情報カテゴリーを書く'),
'menu_name' => __( '新着情報カテゴリー' ),
);
$args = array(
'labels' => $taxonomylabels,
'hierarchical' => true,
'has_archive' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'newscat' )
);
register_taxonomy('newscat','news',$args);
}
// Rewrite Custom Taxonomy to share base slug with Custom Post Type
function rewrite_news_category() {
$news_category_args = get_taxonomy( 'newscat' ); // returns an object
$news_category_args->show_admin_news = true;
$news_category_args->rewrite['slug'] = 'news';
$news_category_args->rewrite['with_front'] = false;
register_taxonomy( 'newscat', 'news', (array) $news_category_args );
}
add_action( 'init', 'rewrite_news_category', 11 );
function news_rewrites_init($post_link, $post = 0){
add_rewrite_rule('news\/([0-9]+)?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&post_type=news&p=$matches[1]', 'top');
add_rewrite_rule('news\/([A-Za-z0-9-_]+)\/?(page\/)?([0-9]+)?\/?$', 'index.php?paged=$matches[3]&newscat=$matches[1]', 'top');
}
add_action('init', 'news_rewrites_init');
function cms_links1($post_link, $post) {
if($post->post_type === 'news') {
return home_url('news/p' . $post->ID . '/');
} else {
return $post_link;
}
}
add_filter('post_type_link', 'cms_links1', 1, 3);
By the adding string "p" in the "rewrite" argument of your custom post type:
'rewrite' => array(
"slug" => "news/p",
"with_front" => false
)
Change the links:
function mycustomname_links($post_link, $post = 0) {
if($post->post_type === 'news') {
return home_url('news/p' . $post->ID . '/');
}
else{
return $post_link;
}
}
add_filter('post_type_link', 'mycustomname_links', 1, 3);
Add the correct rewrites rules:
function mycustomname_rewrites_init(){
add_rewrite_rule('news/p([0-9]+)?$', 'index.php?post_type=news&p=$matches[1]', 'top');
}
add_action('init', 'mycustomname_rewrites_init');
After this you need to flush the permalink:
In your WordPress backend go to settings-> permalinks-> save changes
Now your URL looks like this:
localhost/demo/news/p83(post id)
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?
I just created a custom post type called 'mtl_chapter'. With my script, I assign a post parent of my CPT to the regular 'post' type. So, my CPT are basically the children of my regular posts. I want to change the permalink structure of my CPT from
/base-slug/cpt-post-title/
to
/parent-title/cpt-post-title/
So, It will look like the attachment posts do with the permalink:
/parent-title/attachment-post-title/
My current code is able to change the permalink structure to what I want, but I get
404 not found
when I click the link. Please help me, here is my current code:
function create_posttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => '%parent-post-name%','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5
)
);
}
add_action( 'init', 'create_posttype' );
add_filter('post_type_link', 'mtl_update_permalink_structure', 10, 2);
function mtl_update_permalink_structure( $post_link, $post )
{
if ( false !== strpos( $post_link, '%parent-post-name%' ) ) {
$parent_id = wp_get_post_parent_id($post->ID);
$parent_post = get_post($parent_id);
$slug = $parent_post->post_name;
if ( $slug ) {
$post_link = str_replace( '%parent-post-name%', $slug, $post_link );
}
}
return $post_link;
}
try using
function create_myposttype() {
register_post_type( 'mtl_chapter',
array(
'labels' => array(
'name' => 'Chapters',
'singular_name' => 'Chapter',
'parent_item_colon' => 'Novel Title:',
'add_new' => _x('Add New', 'indomtl'),
'add_new_item' => __( 'Add New Chapter' )
),
'hierarchical' => true,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-format-aside',
'rewrite' => array('slug' => 'mtl_chapter','with_front' => true),
'exclude_from_search' => true,
'show_ui' => true,
'menu_position' => 5,
'supports' => array(
'page-attributes' /* This will show the post parent field */,
'title',
'editor',
),
)
);
}
add_action( 'init', 'create_myposttype' );
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.
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.