I am trying to add terms using code but it's not added into wordpress. I am using below code to add
$term = wp_insert_term('Jogger', 'product_style',0);
Every time it return Invalid taxonomy. I have check my db and I don't found any entry for this. Even I have check with term_exists('Jogger', 'pa_style',0); and it also return 0. Can some have any idea about it.
I am able to add other terms like
$term = wp_insert_term('SPORTS', 'product_cat',0);
Use this code in functions.php
add_action( 'init', 'create_new_taxonomy' );
function create_new_taxonomy() {
register_taxonomy(
'product_style',
'products',
array(
'label' => __( 'Product Style' ),
'rewrite' => array( 'slug' => 'product_style' ),
'hierarchical' => true,
)
);
}
You can't insert term here because your taxonomy is not registered. So use the above code register it first. Then insert term.
Related
To begin with I have created a simple plugin that contains this piece of code:
function woo_create_credit_attribute_taxonomy() {
$attributes = wc_get_attribute_taxonomies();
$slugs = wp_list_pluck( $attributes, 'remaining_credits' );
if ( ! in_array( 'remaining_creds', $slugs ) ) {
$args = array(
'slug' => 'remaining_creds',
'name' => __( 'Remaining Credits', 'bidrop-credits' ),
'type' => 'select',
'orderby' => 'menu_order',
'has_archives' => false,
);
$result = wc_create_attribute( $args );
}
}
// On Activation create the credit attribute taxonomy
add_action( 'admin_init', 'woo_create_credit_attribute_taxonomy' );
This piece of code, simply creates a new Woocommerce Product Attribute.
And in the uninstall.php file of the same plugin, I use the WPDB query below to remove it uppon uninstall.
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE 1");
Notes: The Attribute is succesfully being deleted from the Database, but remains visible on front-end and when I try deleting it from the front-end it seems to be stuck there, Until I deactive and Reactivate Woocommerce.
Any ways I could adjust this code to flush_rewrite the woocommerces rules?
I have already tried the cassual flush_rewrite_rules() built-in function from wordpress. But it has no better results.
Thanks in advance for your time and effort
In the backend of Wordpress I use the default http://localhost/sitename/example-post/ value for creating permalinks.
For custom post types I defined a custom slug this way, here is services for example:
register_post_type( 'service',
array(
'labels' => array(
'name' => __( 'Services' ),
'singular_name' => __( 'Service' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array(
'slug' => 'services',
'with_front' => true
),
'supports' => array(
'title',
'editor',
'excerpt',
'thumbnail'
),
'taxonomies' => array( 'category' ),
)
);
It creates services/post-name.
I also use this hook to create a custom page to create a custom page permalink:
function custom_base_rules() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/page/%pagename%/';
}
add_action( 'init', 'custom_base_rules' );
It creates page/post-name
Now the only thing I need to do is to create another custom permalink path for the normal Wordpress posts.
So the outcome world be for the post type of post:
post/post-name
I can't use the backed for this because I already defined a default way of handling the permalinks. I already managed to rewrite the paths of custom post types and pages...
How do I rewrite the normal post post type permalink path in Wordpress programmatically?
You need to do it in two steps.
First enable rewrite with 'with_front' => true for the buildin post registration
add_filter(
'register_post_type_args',
function ($args, $post_type) {
if ($post_type !== 'post') {
return $args;
}
$args['rewrite'] = [
'slug' => 'posts',
'with_front' => true,
];
return $args;
},
10,
2
);
This way urls like http://foo.example/posts/a-title work but generated links in are now wrong.
Links can be fixed by forcing custom permalink structure for the buildin posts
add_filter(
'pre_post_link',
function ($permalink, $post) {
if ($post->post_type !== 'post') {
return $permalink;
}
return '/posts/%postname%/';
},
10,
2
);
See https://github.com/WordPress/WordPress/blob/d46f9b4fb8fdf64e02a4a995c0c2ce9f014b9cb7/wp-includes/link-template.php#L166
Posts have to use the default permalink structure, they do not have a special entry in the rewrite object in the same way that pages or custom post types do. If you want to change the default structure programmatically, you can add something like this to your hook.
$wp_rewrite->permalink_structure = '/post/%postname%';
I'm not super clear what you mean when you say
I can't use the backed for this because I already defined a default way of handling the permalinks. I already managed to rewrite the paths of custom post types and pages...
It sounds as though you're overriding the default behavior for permalinks everywhere except for posts so if you changed the default it would likely only affect posts anyway.
The permalink_structure property suggested by GentlemanMax did not work for me. But I found a method that does work, set_permalink_structure(). See code example below.
function custom_permalinks() {
global $wp_rewrite;
$wp_rewrite->page_structure = $wp_rewrite->root . '/page/%pagename%/'; // custom page permalinks
$wp_rewrite->set_permalink_structure( $wp_rewrite->root . '/post/%postname%/' ); // custom post permalinks
}
add_action( 'init', 'custom_permalinks' );
I have this URL for my Resort page page-resorts.php:
http://localhost/testwordpress/resorts/
After clicking the link to a post under Resort custom page I will have this URL for my custom page template (CPT) single-resort.php:
http://localhost/testwordpress/resort/kurumba-maldives/
As you can see the resorts was changed to resort because I can't use resorts slug for the slug post.
How can I achive this kind of URL:
http://localhost/testwordpress/resorts/kurumba-maldives/
where the resorts word is used and not resort word only?
I've heard about custom slug and search for it, but I still can't understand it.
You can create your custom post type this way.
function create_posttype() {
register_post_type( 'resort',
array(
'labels' => array(
'name' => __( 'Resorts' ),
'singular_name' => __( 'Resort' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'resorts'),
)
);
}
add_action( 'init', 'create_posttype' );
'rewrite' => array('slug' => 'resorts'), this used to add the slur on URL. this way you can acheive your URL.
Or you can override your current slug in functions.php
add_filter( 'register_post_type_args', 'wpse247328_register_post_type_args', 10, 2 );
function wpse247328_register_post_type_args( $args, $post_type ) {
if ( 'resort' === $post_type ) {
$args['rewrite']['slug'] = 'resorts';
}
return $args;
}
For more, please visit.
register post type
Change Custom Post Type slug
I wanna be able to post my WooCommerce products into my "posts" categories. Based based off this cod below, it's possible. Here's the code I'm using in my functions.php. The categories are clickable when I make a new product in Woo however, it's not posting to the category itself. Appreciate any insight on this matter.
Add category selection to custom post type
function reg_cat() {
register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
}
add_action('init', 'reg_cat');
For register custom taxonomy you can try below code, for more https://codex.wordpress.org/Function_Reference/register_taxonomy
<?php
add_action( 'init', 'create_book_tax' );
function create_book_tax() {
register_taxonomy(
'genre',
'book',
array(
'label' => __( 'Genre' ),
'rewrite' => array( 'slug' => 'genre' ),
'hierarchical' => true,
)
);
}
?>
Please try This Code for register new texonomy
function custom_taxonomy() {
register_taxonomy(
'custom_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'post_type', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'custom_taxonomy');
Note: Please add this code in theme function.php or your plugin
=======================================================================
function reg_cat() {
register_taxonomy_for_object_type('category','CUSTOM_POST_TYPE');
}
add_action('init', 'reg_cat');
This code working post category display in custom post type texonomy section Please refer
Background
I register a custom post type and custom taxonomy inside a class. Inside the WP admin, I see both the post type, and I see the taxonomy.
Simplified Code:
class Custom_Post_Type {
function __construct($init_data) {
if ( is_admin() ) {
add_action( 'init', array( $this, 'create_ctp' ) );
add_action( 'admin_head', array( $this, 'create_ctp_icons' ) );
add_action( 'save_post', array( $this, 'save_ctp_custom_metadata' ), 1, 2 );
}
}
function create_ctp_taxonomy() {
register_taxonomy(
$post_type.'_type',
$post_type,
array(
'labels' => array(
'name' => $taxonomy_label,
'add_new_item' => 'Add New '.$taxonomy_label
),
'public' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true
)
);
register_post_type($post_type_slug,
array(
'labels' => array(),
'public' => true,
'has_archive' => false,
'supports' => $this->supports,
'register_meta_box_cb' => array( $this, 'create_ctp_custom_metaboxes' ),
'taxonomies' => array( $taxonomy_slug ),
)
);
}
}
Again, this works inside the admin area. I can add posts, and I can see the taxonomy and add terms.
Problem
On the front end, get_taxonomies() doesn't see the new custom taxonomy, and get_terms() doesn't see the terms inside it.
I tried several examples of of register_taxonomy, and when I used it outside of the class, it appears on the front end. However, when I moved the examples into my internal create_ctp_taxonomy function, they are suddenly invisible to get_taxonomies.
Any ideas why this would be occurring?
Edit
I've been playing around with different things and I think the issue here is the timing of the init action. When I call the setup function direction from the __construct function, rather than adding an action, then things start working. Example:
class Custom_Post_Type {
function __construct($init_data) {
if ( is_admin() ) {
//add_action( 'init', array( $this, 'create_ctp' ) );
add_action( 'admin_head', array( $this, 'create_ctp_icons' ) );
add_action( 'save_post', array( $this, 'save_ctp_custom_metadata' ), 1, 2 );
}
$this->create_cpt();
}
}
By doing this, I skip using init at all. However, this seems to violate standard practice. Is there a downside that anyone knows of for doing it this way?
There are a couple of things you need to be aware of when registering taxonomies to custom post types.
Register the taxonomies first. This seems a bit counter intuitive but taxonomies are registered to post types, so they need to exist before the post type is created.
You also need to register the taxonomy to the post type using the taxonomies argument of the register_post_type function.
Eg.
register_post_type('cpt_name',array(
'taxonomies'=>array(
'taxomony_name1',
'taxomony_name2')
,other_arguments...)
);
From the docs
When registering a post type, always register your taxonomies using
the taxonomies argument. If you do not, the taxonomies and post type
will not be recognized as connected when using filters such as
parse_query or pre_get_posts. This can lead to unexpected results and
failures
Not Problems
1.) The problem isn't a race condition.
Conditionals like is_admin() still work when run from functions.php directly. This contradicts some information on the web, but as of WordPress 4.4, these do work.
2.) Calling the registration from add_action() rather than directly from __construct()
Switching to calling the registration directly had zero change. To be clear, there is no difference between:
$this->create_ctp()
add_action('init', array( $this, 'create_ctp' ) );
3.) Order of registering taxonomy vs CTP
When I moved my registration of the taxonomy in front of the CTP, it had zero change in behavior.
The Problem
I was using a conditional check of is_admin(), which I'd added previous to wrap when I added the admin dashicon. This is why my CTP was appearing on the backend, but not on the front.
I had removed it from my simplified example, so there was no way to tell from looking at the code that I'd posted.
So all I needed to do was remove the is_admin() check. A silly mistake it turns out, but useful to find the information about what things aren't actually problems.