What part do you need help with?
Thread Starter
nmu1
(@nmu1)
OK here’s what I’m trying in functions.php:
add_action(‘wp_ajax_admin_init’,’my_select_event’);
function my_select_event($post_id){
if(isset($_GET['tag_ID'])){
$my_tag = intval($_GET['tag_ID']);
}
$term_array = get_term_by('id', $my_tag, 'tribe_events_cat');
$term_name = $term_array->term_id;
wp_set_object_terms($post_id, $term_name, 'tribe_events_cat', false);
wp_update_post($post_id);
}
I originally had it so that wp_set_object_terms just used that $_GET['tag_ID'],
and then I just got a checkbox with that number as a label, nothing checked. So I think I’m about halfway there, I just need to know how to manipulate that tag ID so that I can get the correct category selected. Can you help?
Maybe I’m not reading this right, or understanding you, but for what its worth here is what I see:
When that function runs, you <i>might</i> be setting the category for that post. You will need to watch the database and see if it works correctly. But you are not updating the generated page at all. That you will have to do with Javascript, not PHP. If you don’t, when the form is submitted it is submitted with the value un-checked and it gets wiped even if your other function works.
If it were me, I would dump the function you have and just click the box via Javascript. Then when the form submits, the category is set.
If you are worried that your editors/contributors might override your category choice, then you have a bigger problem that will take some thought. Neither your solution nor mine will stop a user from just unchecking the box.
Thread Starter
nmu1
(@nmu1)
Yeah you’re right, I thought I could do it via AJAX but in the end the user can just uncheck the box (or check multiple). I’ll leave it blank as it is now. Thanks for your time and help!
Also looking to do this.
To preselect a category on a new post as users may not select a category and the default category wouldn’t be correct. I’d like to have a link (well lots of links) – add new post – and the new post page has the correct category already selected.
Have googled and searched plugins but no luck.
Any help out there?