3

I'm building a site with two separate CPTs nominees and winners

The idea is that we have a user submit a nomination in one of 7 categories ( taxonomy ) on the front end, and editors go in and approve nominations for display on the website.

Once every quarter, a selection of 7 nominees ( one from each category ) are chosen as winners.

Is there a way to copy the fields from a nominee CPT ( name, department, taxonomy, content ) to a winner CPT? Ideally, this would be done by a call in the Admin.

  • 1
    Would it not make more sense to use a taxonomy that specifies if they're a winner or not? – Tom J Nowell Aug 4 '16 at 20:54
  • It might @TomJNowell but I have a feeling that keeping it separated will end up being a better separation. Winners are essentially going to be curated articles written with some of the content from the nomination. I feel like keeping them separate is a better COA. There will be exactly 42 winners / year and keeping those organized and all together is important. Copying the nomination to a "Winner" CPT seems like a good way to keep it separated both in the Admin and when querying data. They want to vet all nominations to publish just an excerpt and title, but write longer posts on the winners. – Armstrongest Aug 4 '16 at 21:50
  • I agree that separating them in the user interface is great UX! But you don't need a custom post type to do that :) Like the frontend, you can use pre_get_posts to adjust what gets shown, and add a new top level admin menu with filtering of your own choosing ( e.g. winners vs non-winners ) – Tom J Nowell Aug 4 '16 at 22:56
  • There's a good plugin by John James Jacoby, one of WP's core developers, at en-gb.wordpress.org/plugins/post-type-switcher – Andy Macaulay-Brook Aug 4 '16 at 23:00
  • 1
    Custom fields were mentioned too. If you change a post's post type, it will still retain all of its custom fields. – Andy Macaulay-Brook Aug 4 '16 at 23:15
3

You can update a posts type using wp_update_post:

$my_post = array(
    'ID'        => $post_id,
    'post_type' => 'winner',
);
wp_update_post( $my_post, true );

// check if it failed and tell the user why
if ( is_wp_error( $post_id ) ) {
    $errors = $post_id->get_error_messages();
    foreach ( $errors as $error ) {
        echo 'error: '.esc_html( $error );
    }
}

Where $post_id is the ID of the post you're switching to

| improve this answer | |
  • Thanks for this. I'm assuming Custom Fields are maintained? – Armstrongest Aug 4 '16 at 23:38
  • No new post is created, it's exactly the same post, same ID, etc, only the post_type field has been changed, there's no reason post meta would be affected. It's the same function used when you edit a post in the admin UI – Tom J Nowell Aug 5 '16 at 11:37
0

Someone else posted a link to this plug-in, but the answer disappeared. I'm not sure why ( is it against the rules to post a link to a plug-in or something? ).

Anyway, I have the link open, so I'm posting it. I think it may solve my issue. I don't know if it will handle Custom Fields yet, but it looks promising.

| improve this answer | |
  • Answers should be self contained, else they become useless if the URL linked to changes or the content changes – Tom J Nowell Aug 4 '16 at 22:57
  • I concur with Tom. I've reposted as a comment above and I'll add another comment up there about custom fields. – Andy Macaulay-Brook Aug 4 '16 at 23:14
  • Thanks @TomJNowell that's true... and I agree with that. Too many dead links on the interwebs. Still a pretty valuable link, but yes better as a comment. – Armstrongest Aug 4 '16 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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