0

I want to add a custom field in the wordpress custom post type, such that this field dynamically fetches the value of the author role.

Purpose -

I want to add this custom field so that I can filter custom post types based on the user role of the author of the posts, that is editor, subscriber, etc.

What have I tried?

Well, I looked up the solution on Internet but couldn't find one and I am not that great in PHP so couldn't give it a shot myself.

Kindly suggest a solution.

Thanks a lot, in advance.

4
  • In general, that's something which would be coded into the theme - no custom field involved, the template in question would simply display the post author's role. It may be possible to inject this data in certain locations independent of theme via certain filters, however. But as it stands, your question may be a little too broad for our site - there's not a clearly phrased question in the post, and no definitive answer to problem posed in the information given thus far. Please take a minute to review the help center section of this stack.
    – bosco
    Commented May 3, 2022 at 3:32
  • I want to add a custom field to the custom post type, that dynamically fetches the user role of the author of the post, and this field I want to use as a filter so as to filter frontend posts on the basis of the user role of the author. Technically a new column of author role with the user role of the respective author must be created in the MySQL database. Kindly let me know in case any further clarification is needed. Thanks.
    – Malani
    Commented May 3, 2022 at 3:39
  • Custom fields (post meta) are statically stored in the database and are associated with individual posts (not post types). You could store the author's role(s) in a custom field on the post when it's saved, dynamically fetch it and display it when a post is served via a filter, or customize an SQL query to dynamically select posts from authors with a certain role. You might consider hiring a developer or finding a plugin which might offer the functionality which you're looking for - I think "WP Faucet" can do it, IIRC
    – bosco
    Commented May 3, 2022 at 4:50
  • Are you referring to facetWP? Well, I simply have to add a post meta where Author role is also a field along with Author ID. And I have looking for several plugins but none have come to my use, including facetWP. And I just need to create this custom field that dynamically fetches value of author role, rest filtering can be done with so many plugins already available. If you can show us a way to achieve this, would be thankful.
    – Malani
    Commented May 3, 2022 at 5:41

1 Answer 1

0

You can hook into save_post_{$post->post_type} which fires after the post has been saved in database.

function wpse405375_save_author_role_as_meta( $post_ID, $post, $update ) {
    
    //Get the User object from author id.
    $author = get_user_by( 'ID', $post->post_author );
    
    //Get author roles
    $roles = $author->roles;
    
    //Store the role/roles in post meta
    
    /* This one will store all the roles as serialized array. */
    update_post_meta( $post_ID, '_author_roles', $roles );
    
    /* If you want just the first role as string (in case the user is associated with multiple roles)
     * Or, in your case, use it for a simple meta_query
    */
    update_post_meta( $post_ID, '_author_role', $roles[0] );
    
}
add_action( 'save_post_MYCPT', 'wpse405375_save_author_role_as_meta', 10, 3 );

Replace MYCPT with your CPT.

EDIT : 07-05-2022

function wpse405375_change_user_posts_meta(  $user_id, $role, $old_roles ) {
    
    //Get User's posts first
    $posts = get_posts(array(
        'post_type' => 'MYCPT', //Change it to your CPT
        'posts_per_page' => -1,
        'author' => $user_id,
    ));
    
    //Loop through the posts and update meta
    if ( $posts && !empty($posts) ) {
        foreach( $posts as $post ) {
            update_post_meta( $post->ID, '_author_role', $role );
        }
    }
}
add_action( 'set_user_role', 'wpse405375_change_user_posts_meta', 10, 3);
10
  • Hi, Thanks for your reply. I added the above code using the code snipped plugin, but it did not create either a new column in the MySQL database or a new field in the post type. Did I miss anything? Thanks for your support. PS - can we connect directly for this?
    – Malani
    Commented May 4, 2022 at 5:09
  • It adds a meta data in the post meta table. You can use that meta field to query posts.
    – Abhik
    Commented May 4, 2022 at 5:32
  • Yup, you are right, it has been added to the post meta, but there is a problem. When I am changing the user role the author role is not getting changed in the post meta. Basically, it is a membership site, when the membership subscription expires, the user role changes and only one who has paid, their listings are available in the front end. The value should change or update on the change of user role. How can we achieve that? Thanks for your answers, you support is appreciated.
    – Malani
    Commented May 4, 2022 at 15:55
  • Well, in that case you can use the set_user_role hook which fires when a user role is changed. Use that to query posts by that author and update the posts with the new meta value.
    – Abhik
    Commented May 5, 2022 at 6:47
  • Hi, my knowledge of PHP isnt that good, how to use this hook? what I tried - add_action( 'save_post_MYCPT', 'set_user_role', 'wpse405375_save_author_role_as_meta', 10, 3 ); But this doesn't seem to be working in the backend. Any thoughts on this?
    – Malani
    Commented May 5, 2022 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.