Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 ?

share|improve this question
    
Why don't you use categories for this?? one for sold and one for new... –  Nasir Zia Jun 9 '12 at 6:56
2  
The Post Type Switcher Plugin just builds an interface around the set_post_type() function. This function changes the post type an entry in the wp_posts table. It does not change the post ID, nor does it touch the wp_postmeta table. Could you explain what exactly you want to do? –  Geert Jun 9 '12 at 7:18
    
I'm trying to list 'make' taxonomy but it show me booth sold cars and new cars....I want just new cars, It doesn't matter if if under separate category or post type, I just don't know how to list this things separately... –  Santoro Jun 9 '12 at 8:32
add comment

1 Answer

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 )
share|improve this answer
    
Stephen Harris, your explication was awesome!! I've registered that taxonomy, like you said, and added the metaboxes, but ... I didn't understand that query... I tried to list all makes under 'car_status' new, but I got only the permalink... –  Santoro Jun 9 '12 at 10:23
    
@Santoro - if you are using the second approach you don't need to re-add the metaboxes etc - there is only one post type, but with two taxonomies. My advice is to ignore the first part, and register a 'car_status' taxonomy for your custom post type. Then for each car give it the term 'new' or 'sold'. Each car (post type) should have a 'make' term and 'car_status' term. –  Stephen Harris Jun 9 '12 at 10:47
    
thanks for you quick answer... Sorry, but I dont know what function ise to list my makes taxonomy using car_status as conditional...should I use get_terms_by ? –  Santoro Jun 9 '12 at 11:22
    
Ok! I found a way to list my taxonomy terms, but...because its looping inside used cars, there are several used cars under 'BMW' make, and it shows me all of that...like BMW BMW BMW MERCEDES MERCEDES JAGUAR There is a way to display only one of these ? –  Santoro Jun 10 '12 at 8:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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