Wordpress custom taxonomy display name issue - wordpress

i have created custom taxonomy named Locations , everything is working fine, but whenever i add a taxonomy, everywhere is showing the taxonomy name as Category not as Location, Like Add New category and so on , in wp-admin
function reg_location_taxonomy() {
register_taxonomy( 'location', array( 'product' ), array( 'hierarchical' => true, 'label' => 'Locations', 'singular_label' => 'Location', 'rewrite' => true ) );
// register_taxonomy_for_object_type( 'location', 'product' );
}
add_action( 'init', 'reg_location_taxonomy', 0 );

You need add label vaue to following array
$labels = array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'menu_name' => __( 'Location' ),
);
like as follows
function reg_location_taxonomy() {
register_taxonomy( 'location', array( 'product' ), array( 'hierarchical' => true, 'label' => $labels, 'singular_label' => 'Location', 'rewrite' => true ) );
// register_taxonomy_for_object_type( 'location', 'product' );
}
add_action( 'init', 'reg_location_taxonomy', 0 );

Related

Taxonomy returns invalid taxonomy error when using get_terms()

I have registered a custom taxonomy.Please find the code below
function register_taxonomy_state() {
$labels = array(
'name' => _x( 'States', 'taxonomy general name' ),
'singular_name' => _x( 'State', 'taxonomy singular name' ),
'search_items' => __( 'Search State' ),
'all_items' => __( 'All States' ),
'parent_item' => __( 'Parent States' ),
'parent_item_colon' => __( 'Parent States:' ),
'edit_item' => __( 'Edit State' ),
'update_item' => __( 'Update State' ),
'add_new_item' => __( 'Add New State' ),
'new_item_name' => __( 'New State Name' ),
'menu_name' => __( 'State' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' =>true,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => [ 'slug' => 'alumns-reps-states' ],
);
register_taxonomy( 'alumns-reps-states', [ 'alumns-reps' ], $args );
}
add_action( 'init', 'register_taxonomy_state' );
I would like to display all terms added under this taxonomy, and for that i used following code
$taxonomies = get_terms( array(
'taxonomy' => 'alumns-reps-states',
'hide_empty' => false
) );
var_dump($taxonomies);
It is always returning 'Invalid Taxonomy' error. WHat is wrong with this code ?

wordpress - query custom post type by category slug

I'm trying to query a custom post type by its category slug, but its not working at all, it always return blank - like no results
my custom post type definitions:
add_action( 'init', 'custom_post_albuns' );
// The custom function to register a movie post type
function custom_post_albuns() {
// Set the labels, this variable is used in the $args array
$labels = array(
'name' => __( 'Albuns' ),
'singular_name' => __( 'Album' ),
'add_new' => __( 'Add New Album' ),
'add_new_item' => __( 'Add New Album' ),
'edit_item' => __( 'Edit Album' ),
'new_item' => __( 'New Album' ),
'all_items' => __( 'All Albuns' ),
'view_item' => __( 'View Album' ),
'search_items' => __( 'Search Albuns' ),
'featured_image' => 'Featured Image',
'set_featured_image' => 'Add Featured Image'
);
// The arguments for our post type, to be entered as parameter 2 of register_post_type()
$args = array(
'labels' => $labels,
'description' => 'm6 Records Albuns',
'public' => true,
'menu_position' => 6,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
'has_archive' => true,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-album',
'query_var' => 'album'
);
// Call the actual WordPress function
// Parameter 1 is a name for the post type
// Parameter 2 is the $args array
register_post_type( 'album', $args);
}
add_action( 'init', 'create_album_taxonomies', 0 );
function create_album_taxonomies() {
$labels = array(
'name' => _x( 'Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Category', 'taxonomy singular name' ),
'search_items' => __( 'Search Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
);
$args = array(
'hierarchical' => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'albuns' ),
);
register_taxonomy( 'albuns_categories', array( 'album' ), $args );
}
and my query:
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' )
)
)
);
and I can't bring a list of albuns (if I remove the tax_query part, it returns to me all the "albuns" custom post type).
Do someone have any ideias???
Thanks!!
You can try
$argsPost = array(
'post_type'=>'album',
'orderby'=>'date',
'order' => 'DESC',
'posts_per_page'=>'4',
'tax_query' => array(
array(
'taxonomy' => 'albuns_categories',
'field' => 'slug',
'terms' => array( 'ANYTERM' ),
'operator' => 'IN'
)
)
);

Show only specific categories in permalinks for custom post type in WordPress

I tried to ask this question in the Wordpress Devlopment network with no success, I'm trying to display only 3 specific categories in permalinks for custom-post-type categories.
Right now the permalinks structure (that is the structure given by the theme i'm using and that i'm modifying through a child theme)is as follows:
www.myfoodblog.com/recipes/the-post-title/
^ ^ ^
root custom-post-type title
some post are under 3 categories that i would like to display in permalinks which are restaurant, users and admin to get something like this
www.myfoodblog.com/recipes/restaurant/the-post-title/
www.myfoodblog.com/recipes/users/the-post-title/
www.myfoodblog.com/recipes/admin/the-post-title/
leaving the original structure for posts that are not in those categories.
I tried using this plugin but it will display the custom post type category for all posts.
I also tried to follow the instructions provided in this question but it's not working, no changes are made in the permalinks structure.
Any advice is much appreciated.
You have to work with both post_type_link and add a rewrite rule
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = get_the_terms( $post->ID, 'recipe-category' );
foreach($terms as $term) {
if( $term->slug == 'restaurants' || $term->slug == 'users'){
return str_replace( site_url()."/recipes" , site_url()."/recipes/".$term->slug , $post_link );
}
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
function custom_rewrite_basic() {
add_rewrite_rule('^recipes/(.+)/(.+)', 'index.php?recipe=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');
Create Post Type
add_action( 'init', 'codex_recipes_init' );
/**
* Register a recipes post type.
*
* #link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function codex_recipes_init() {
$labels = array(
'name' => _x( 'Recipes', 'post type general name', 'your-plugin-textdomain' ),
'singular_name' => _x( 'Recipe', 'post type singular name', 'your-plugin-textdomain' ),
'menu_name' => _x( 'Recipes', 'admin menu', 'your-plugin-textdomain' ),
'name_admin_bar' => _x( 'Recipe', 'add new on admin bar', 'your-plugin-textdomain' ),
'add_new' => _x( 'Add New', 'recipe', 'your-plugin-textdomain' ),
'add_new_item' => __( 'Add New Recipe', 'your-plugin-textdomain' ),
'new_item' => __( 'New Recipe', 'your-plugin-textdomain' ),
'edit_item' => __( 'Edit Recipe', 'your-plugin-textdomain' ),
'view_item' => __( 'View Recipe', 'your-plugin-textdomain' ),
'all_items' => __( 'All Recipes', 'your-plugin-textdomain' ),
'search_items' => __( 'Search Recipes', 'your-plugin-textdomain' ),
'parent_item_colon' => __( 'Parent Recipes:', 'your-plugin-textdomain' ),
'not_found' => __( 'No recipes found.', 'your-plugin-textdomain' ),
'not_found_in_trash' => __( 'No recipes 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' => 'recipes/%type%' ),
'capability_type' => 'post',
'has_archive' => 'recipes',
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'recipes', $args );
}
Important: look on rewrite rule: 'rewrite'=> array( 'slug' => 'recipes/%type%' ),
Create Custom Taxonomy
// hook into the init action and call create_recipes_taxonomies when it fires
add_action( 'init', 'create_recipes_taxonomies', 0 );
function create_recipes_taxonomies() {
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Type', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Type', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Types', 'textdomain' ),
'popular_items' => __( 'Popular Types', 'textdomain' ),
'all_items' => __( 'All Types', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Type', 'textdomain' ),
'update_item' => __( 'Update Type', 'textdomain' ),
'add_new_item' => __( 'Add New Type', 'textdomain' ),
'new_item_name' => __( 'New Type Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate types with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove types', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used types', 'textdomain' ),
'not_found' => __( 'No types found.', 'textdomain' ),
'menu_name' => __( 'Types', 'textdomain' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'type' ),
);
register_taxonomy( 'type', 'recipes', $args );
}
Add Filter Post Type Link
function recipes_post_link( $post_link, $id = 0 ){
$post = get_post( $id );
if ( is_object( $post ) ){
$terms = wp_get_object_terms( $post->ID, 'type' );
if( $terms ){
return str_replace( '%type%' , $terms[0]->slug , $post_link );
}
}
return $post_link;
}
add_filter( 'post_type_link', 'recipes_post_link', 1, 3 );
Important: After all go to permalink options and reset it.

Why is wordpress making a query for attachment instead of taxonomy?

I made a custom post type and I've made a taxonomy registered to the cpt and the standard 'post'. This all works perfect.
Now I needed a 2nd taxonomy for organization reasons. I've simply copied the first one and changed the name and labels.
On the backend it all works normal, but on the front end clicking on it redirects to index.php. If I look via the debug toolbar at the query it is attachment=(term-name) instead of (taxonomy)=(term-name). Does anybody have any idea why wordpress does this? (Or to put it better, how did I make wordpress do this?)
This is how the taxonomies are registered, 'arweb' is the cpt and 'onderwerp' is the normal working taxonomy:
add_action( 'init', 'register_custom_taxonomies' );
function register_custom_taxonomies(){
// ///////////////////// ONDERWERP //////////////////////
$labels = array(
'name' => _x( 'Onderwerpen', 'taxonomy general name' ),
'singular_name' => _x( 'Onderwerp', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Onderwerpen' ),
'popular_items' => __( 'Popular Onderwerpen' ),
'all_items' => __( 'All Onderwerpen' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Onderwerp' ),
'update_item' => __( 'Update Onderwerp' ),
'add_new_item' => __( 'Nieuw Onderwerp' ),
'new_item_name' => __( 'Naam Nieuw Onderwerp' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Onderwerpen' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'onderwerp' ),
);
register_taxonomy( 'onderwerp', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp', 'arweb' );
///////////////////// English onderwerp /////////////////////////////////////////////
$labels = array(
'name' => _x( 'Subjects', 'taxonomy general name' ),
'singular_name' => _x( 'Subject', 'taxonomy singular name' ),
'search_items' => __( 'Zoek Subjects' ),
'popular_items' => __( 'Popular Subjects' ),
'all_items' => __( 'All Subjecten' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Subject' ),
'update_item' => __( 'Update Subject' ),
'add_new_item' => __( 'Nieuw Subject' ),
'new_item_name' => __( 'Naam Nieuw Subject' ),
'add_or_remove_items' => __( 'Add or remove onderwerpen' ),
'choose_from_most_used' => __( 'Choose from the most used onderwerps' ),
'not_found' => __( 'No onderwerps found.' ),
'menu_name' => __( 'Subjecten' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
//'update_count_callback' => '_update_post_term_count',
'query_var' => true,
//'rewrite' => array( 'slug' => 'english-subject' ),
);
register_taxonomy( 'onderwerp_en', array( "arweb","post" ), $args );
register_taxonomy_for_object_type( 'onderwerp_en', 'post' );
register_taxonomy_for_object_type( 'onderwerp_en', 'arweb' );
}
Try updating your permalinks (by going to Settings .. Permalinks and saving). It's possible the URL is matching an outdated rewrite rule.

Custom taxonomy archive doesnt work

here is my registerin taxonomy function to my posts.
add_action( 'init', 'marka_taxonomies', 0 );
function marka_taxonomies()
{
$labels = array(
'name' => _x( 'Markalar', 'taxonomy general name' ),
'singular_name' => _x( 'Marka', 'taxonomy singular name' ),
'search_items' => __( 'Markalarda Ara' ),
'all_items' => __( 'Tüm Markalar' ),
'parent_item' => __( 'Alt Marka Kategorisi' ),
'parent_item_colon' => __( 'Üst Marka Kategorisi:' ),
'edit_item' => __( 'Markayı Düzenle' ),
'update_item' => __( 'Markayı Güncelle' ),
'add_new_item' => __( 'Yeni Marka Ekle' ),
'new_item_name' => __( 'Yeni Marka Adı' ),
'menu_name' => __( 'Marka' )
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'marka' )
);
register_taxonomy( 'marka', array( 'post' ), $args );
}
Thnik everthing is normal but the taxonomy-marka.php doesnt work. I still get 404 Not found on title. And there is nothing on my taxonomy archive page. What should i do also where i am wrong ?
When I updated my permalinks option the taxonomy-marka.php page listed my posts.

Resources