• Hi there,
    don´t know if the solution is obvious but I don´t find it:

    I am looking for a solution to either show a certain branch of wordpress-categories as an ACF-Select-Field or to auto-add a post with a certain ACF-Value to a WP-category when saving the post (WP-category has also to be deselected when deselecting the ACF-value).

    My goal:
    Our posts can be in multible WP-categories of course. I wan´t force the author to select a leading category on each post wich I can display in lists etc.

Viewing 1 replies (of 1 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    This is a question related to ACF, and not to the plugin itself, but here is a possible solution:

    Use the hook acf/save_post (https://www.advancedcustomfields.com/resources/acf-save_post/) in order to manipulate the post behavior once saved. In your example:

    
    add_action('acf/save_post', 'acf_save_post_category', 20)
    function acf_save_post_category($post_id){
        
        // Target the specific post type: Post
        if(get_post_type($post_id) != 'post')
            return;
        
        // Get current post categories
        // Note: If using a custom post type & custom taxonomy, you must use wp_get_post_terms()
        $current_categories = get_the_category($post_id);
        
        // If categories are set: Stop
        if(!empty($current_categories))
            return;
        
        // No category is set. Set default category ID: 1
        // Note: If using a custom post type & custom taxonomy, you must use wp_set_object_terms()
        wp_set_post_categories($post_id, array(1));
    
    }
    

    This will check if a post hs no category, and set a default category ID if that’s the case.

    Hope it helps!

    Regards.

Viewing 1 replies (of 1 total)

The topic ‘ACF and WP-Categories’ is closed to new replies.