• Resolved jimi111

    (@jimi111)


    Hello,
    Can’t get these fields to work.
    This is what I tried
    Create a new field: Field Type Post Statuses, Radio
    Go to a post, lets say it has Private Status
    The field loaded with Publish Status
    Select Trash or Publish status, click Update
    Nothing is changed, the field value is now Publish, but the post status remains the same.

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

    (@hwk-fr)

    Hello,

    Thanks for the feedback. The “Post Statuses” & “Post Types” fields are simply selector fields, they don’t automatically update the current Post.

    Those fields are meant to developers which can easily embed the selector of their choice. That’s why you have multiples settings like: “Select multiples post types”, when it is not possible to actually have multiple post types for on single post.

    I added the feature request to the Trello Board to let users automatically update the current post: https://trello.com/c/rOHK8pot/334-fields-post-types-statuses-setting-to-update-the-current-post

    Have a nice day!

    Regards.

    Thread Starter jimi111

    (@jimi111)

    Thanks for the fast reply! Now I see, so at a current state they are just a value loaders for developers.
    >I added the feature request to the Trello Board to let users automatically update the current post
    Would be great!

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Exactly! You can load/save the value to update the current post by yourself right now, using the following code:

    
    /*
     * Field: My Post Type - Load Value
     */
    add_filter('acf/load_value/name=my_post_type', 'my_acf_post_type_load', 10, 3);
    function my_acf_post_type_load($value, $post_id, $field){
        
        // Bail early if not in a Post
        if(!is_numeric($post_id))
            return $value;
        
        // Retrieve Current Post Type
        $post_type = get_post_type($post_id);
        
        // Force the Field value to load the Current Post Type
        $value = $post_type;
        
        // Return
        return $value;
    
    }
    
    /*
     * Field: My Post Type - Save Value
     */
    add_filter('acf/update_value/name=my_post_type', 'my_acf_post_type_save', 10, 3);
    function my_acf_post_type_save($value, $post_id, $field){
        
        // Bail early if not in a Post
        if(!is_numeric($post_id))
            return $value;
        
        // Update Current Post to the selected Post Type
        wp_update_post(array(
            'ID'        => $post_id,
            'post_type' => $value,
        ));
        
        // Return null to not save the value as a meta
        return null;
    
    }
    

    This is a simple example for the “Post Types” field selector.

    Hope it helps!

    Regards.

    Thread Starter jimi111

    (@jimi111)

    Thanks a lot for the code! For me it would take probably a few hours to write this.
    And especially for reducing database bloating with useless values
    // Return null to not save the value as a meta

    In case someone with very low programming skills like me need it for the post status.

    /*
     * Field: My Post Status - Load Value
     */
    add_filter('acf/load_value/name=post_status', 'acf_post_status_load', 10, 3);
    function acf_post_status_load($value, $post_id, $field){
    
        // Bail early if not in a Post
        if(!is_numeric($post_id))
            return $value;
        
        // Retrieve Current Post Status
        $post_status = get_post_status($post_id);
        
        // Force the Field value to load the Current Post Status
        $value = $post_status;
    
        // Return
        return $value;
    
    }
    
    /*
     * Field: My Post Status - Save Value
     */
    add_filter('acf/update_value/name=post_status', 'my_acf_post_status_save', 10, 3);
    function my_acf_post_status_save($value, $post_id, $field){
        
        // Bail early if not in a Post
        if(!is_numeric($post_id))
            return $value;
        
        // Update Current Post to the selected Post Status
        wp_update_post(array(
            'ID'        => $post_id,
            'post_status' => $value,
        ));
        
        // Return null to not save the value as a meta
        return null;
    
    }
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Great! 🙂

    Regards.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Post Status, Post Type fields’ is closed to new replies.