Skip Navigation

[Resolved] Custom Taxonomy

This support ticket is created 2 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Bob 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2140995

Bob

I have used standard categories for a custom post type which is now causing me issues so I want to swap over to a custom taxonomy.
Two questions - firstly how do I programmatically set a custom taxonomy? - the equivalent to 'post_category' => array(21)
Secondly is there a way to transfer all the current categories that have been set to the custom taxonomy? They are all top level so no hierarchy to worry about.

#2141327

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

Thank you for getting in touch.

Unfortunately any solution for this will need to be done using custom coding or with a plugin.

I was able to find this plugin below after doing some searching.
hidden link

Secondly another solution in the code below.
https://wordpress.stackexchange.com/questions/187832/copy-move-selected-taxonomy-terms-to-another-taxonomy-for-posts

Please let me know if this helps.
Thanks,
Shane

#2141691

Bob

Thanks Shane I thought as much.
How about the first bit - how do I programmatically set a custom taxonomy? - the equivalent to 'post_category' => array(21)

#2142019

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

I suspect you are referring to setting a post taxonomy on the post itself. In this case you will use the function below.
https://developer.wordpress.org/reference/functions/wp_set_post_terms/

This will allow you to programmatically assign the taxonomies to your post.

Thanks,
Shane

#2142483

Bob

Thanks Shane.
How would I refer to the custom taxonomy Does it have a prefix like wpcf_ or is it just what I've called it?
For example I currently do this
$posttitle = 'Item-'.$postdate;
if (!post_exists( $posttitle,'','','loan'))
{$item_loan = array(
'post_title' => $posttitle,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'loan',
'post_category' => array(21),
);
$result_loan = wp_insert_post( $item_loan );

So for the custom taxonomy "loan-category" what would I use instead of 'post_category' => array(21)?

#2143807

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Bob,

No the custom taxonomies don't use the prefix wpcf-

Secondly i checked the wordpress post object attributes and i'm not seeing one called 'post_category'

The correct way to set the taxonomy to a post is by using wp_set_post_terms() function.

You should be able to do it like this.

$posttitle = 'Item-'.$postdate;
if (!post_exists( $posttitle,'','','loan'))
{$item_loan = array(
'post_title' => $posttitle,
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'loan',
);
$result_loan = wp_insert_post( $item_loan );
wp_set_post_terms($result_loan->ID, 'term','loan-category');

This would be the correct way to set the terms. In the example above you seen that i've added 'term', this would be the slug of the term that you want to set.

Thanks,
Shane

#2146495

Bob

Thanks Shane - I haven't tested this fully but it doesn't seem to be working. I've had a quick look and think perhaps I need to use wp_set_object_terms as oppose to wp_set_post_terms as this is a custom post but that could be a red herring.
May I leave the ticket open until I can try out your suggestion more completely.

#2146583

Bob

I got there in the end thanks Shane.
I'm using 'tax_input' => array('loan-category' => 25),

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.