How to move a post to different post type with all meta data?

I'm trying to move a post from a custom post type called 'new cars' to 'sold cars' using the Post Type Switcher plugin, and the plugin works well, but the all meta data associated with this post still belonging to 'new cars' post type...

So, the problem appears when I list categories from 'new cars' post type and that 'sold car' make, model, colour and other metas still there...

Someone have an advice ?

Topic switch post-meta custom-post-types Wordpress

Category Web


As has been mentioned in the comments - changing the post type of a post only changes its 'post type' column. All post metadata is associated with the post ID and this never changes.

However, you may not see this information: if the old post type had a metabox or taxonomy associated with that, that the new post type doesn't - then while the relationship of the converted post with its metadata / taxonomies still exists in the database, it won't appear in the admin screen.

If you have any metaboxes or taxonomies for your 'new cars' post type, you'll want to register them with your 'sold cars' too. For taxonomies this is done by specifying the post types when registering:

register_taxonomy('my-taxonomy',array('new_cars','sold_cars', $args);

Or when registering a custom post_type (see register_post_type()), or by using register_taxonomy_for_object_type().

Similarly you'll want to add your metaboxes to both post types:

add_meta_box( $id, $title, $callback, 'new_cars', $context, $priority, $callback_args )
add_meta_box( $id, $title, $callback, 'sold_cars', $context, $priority, $callback_args )

and ensure that the associated processing runs for both post types.


However - from what it seems you're after, you should be using a post type 'cars' and register a taxonomy (say 'car_status'), as well as you're existing 'make' taxonomy.

Then a car can be associated to a car status term (say 'new' or 'sold').

To get all new cars of make xyz you can use the tax_query argument of get_posts or WP_Query

$args = array(
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'car_status',
            'field' => 'slug',
            'terms' => array( 'new')
        ),
        array(
            'taxonomy' => 'make',
            'field' => 'slug',
            'terms' => array( 'xyz')
        )
    )
);
$query = new WP_Query( $args )

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.