I have authors posting posts with a custom post type and have users with Editor roles publishing them. How can I get the editors who published them?
2 Answers
As far as I know, WordPress only stores original authors of the posts. If you want to also save the publisher's ID, you need to do a bit of custom coding, or use some 3rd party plugin.
Essentially what you need to do is to store the user ID whenever the post status is being changed to publish
. For that you can use the publish_{your_post_type}
action (replace {your_post_type} with your custom post type slug). If your CPT slug is project
for example, your action should look like this:
function save_project_publisher_meta( $post_id, $post ) {
// Set current user ID as a post meta
update_post_meta( $post_id, 'publisher_id', get_current_user_id() );
}
add_action( 'publish_project', 'save_project_publisher_meta', 10, 3 );
Now the publisher ID should be stored as a post meta for each post, and you can access the user data whenever you need by retrieving the publisher ID using get_post_meta
:
// Get WP_User object from ID
$publisher_id = get_post_meta( $post_id, 'publisher_id' );
$publisher = get_user_by( 'id', $publisher_id );
// Get whatever user data you need
$publisher_email = $publisher->user_email;
Note this will only affect newly published posts. Posts that were published in the past won't have publisher IDs stored, until you re-publish them.
wordpress don't have native functionality to handle changes around post/page from users. You need to write / or use plugin to get them and control your authors.
-
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.– Community BotOct 14, 2022 at 7:23