Skip Navigation

[Resolved] How to attach default taxonomy term to post when saved

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to attach default taxonomy term to post when saved

Solution:
You can use "save_post" action and it must be used with a priority of at least 30 to assign default taxonomy term to post.

You can find the proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/i-cant-find-away-to-select-a-taxonomy-option-as-default/#post-1166193

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

This support ticket is created 5 years, 11 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.

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

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 9 replies, has 2 voices.

Last updated by stephenW-4 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1165300
Capturenotification.PNG
Capture1.PNG

I am trying to: Create a custom taxonomy, the taxonomy will just have one option (possibly two).

My question is how can I select an option by default?

My scenario is a notification system. When a new post is published an email is sent to a list of subscribers, I want this to be the default action with the ability to turn the option off if required on a post by post basis.

My second question is how to remove unwanted functions in the metabox. Add new notification and Most Used are not required, I guess it could be done via CSS but wondered if there is another way.

Please see attached images for an explanation.

best regards

Stephen

#1165330

To select a taxonomy term by default I have placed a PHP snippet I have in the functions.php file

The CPT name is event, the taxonomy name is send-to-subscriber and the default slug name I want to select is send. Any suggestions as to why this does not work?

function mfields_set_default_object_terms( $post_id, $post ) {
if ( ‘publish’ === $post->post_status && $post->post_type === ‘event’ ) {
$defaults = array(
‘send-to-subscriber’ => array( ‘send’ )
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( ‘save_post’, ‘mfields_set_default_object_terms’, 100, 2 );

#1165476

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - the feature you are looking for is known to us and we already filed a feature request and its open to upvote:
=> https://toolset.com/feature-request/set-a-default-taxonomy-term-for-new-custom-posts/

You can share your vote to make it a popular feature request.

Now, regarding save_post hook, what if you try to use the following code:

function mfields_set_default_object_terms( $post_id, $post ) {

         if ( 'publish' == $post->post_status && $post->post_type == 'event' ) {
                         wp_set_object_terms($post_id,'send', 'send-to-subscriber',true);
         }

}
add_action( 'save_post', 'mfields_set_default_object_terms', 100, 2 );

More info:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

#1166104

hello, thanks for your response, unfortunately, it hasn't solved the issue. The code works and does set the custom taxonomy, but it fails to trigger the notification. I'm guessing this is something to do with timing/order of actions.

i have tried giving the action a higher priority but this failed to work also.
add_action( 'save_post', 'mfields_set_default_object_terms', 0, 2 );

Is there anything else you can suggest that would make this action trigger first before anything else or can a metabox be set using javascript client side.

thanks

Stephen

#1166169

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - you need to give use priority at least 30.

The general save_post action must be used with a priority of at least 30 when used in conjunction with post types registered with Types, or inconsistencies in the updated data may be experienced.

So, could you please share problem URL and access details, I will try to fix this for you.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#1166181

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry but I wanted to double check your requirement here.

I'm on following page which is used to create new club-news post and I see there is taxonomy available to select.
hidden link

As I understand you want to attach " Send now" term automatically when user create new post for post type "club-news' - correct? Is there any other additional requirement you have?

#1166184

No additional requirement.

Setting the taxonomy should trigger the Mailster newsletter plugin into sending an email to a list of subscribers. However, It only works if the taxonomy is set manually. The action you supplied must be triggering after the call to the newsletter plugin I think.

#1166188

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok thank you. Unfortunately, FTP access details not working at this end. Additionally, I do not have access to theme editor, that means I do not able to access the theme files.

Could you please send me working FTP access details.

I have set the next reply to private which means only you and I have access to it.

#1166193

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing working access details.

I've adjusted the code to the functions.php file as given under and I can see its working. Could you please confirm:


function mfields_set_default_object_terms( $post_id, $post ) {


         if ($post->post_type == 'club-news') {

                   wp_set_object_terms($post_id,'send-now', 'send-to-subscriber',true);
         }
}

add_action( 'save_post', 'mfields_set_default_object_terms', 30, 2 );
#1166197

My issue is resolved now. Thank you!