WordPress.org

Ready to get started?Download WordPress

Forums

Using URLs and AJAX to pre-select a category on edit-post (6 posts)

  1. nmu1
    Member
    Posted 1 year ago #

    If I append &tag_ID=8 to a URL that someone clicks to get to my post-new.php page, can I use that &tag_ID to specify a category that should be checked on arrival to that page, and check it using admin-ajax?

    I want users who are logged in to click on an 'add post' link with a certain term ID appended, and when they get to that page that term will be checked (I am using a custom taxonomy).

    Thanks for any help in advance.

  2. s_ha_dum
    Member
    Posted 1 year ago #

    What part do you need help with?

  3. nmu1
    Member
    Posted 1 year ago #

    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?

  4. s_ha_dum
    Member
    Posted 1 year ago #

    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.

  5. nmu1
    Member
    Posted 1 year ago #

    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!

  6. debsch
    Member
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic