Convert posts to custom post types
-
Hello!
Is possible to convert normal posts to custom posts types?
I want to use custom posts types in an old website where all items were inserted as posts.
Is this possible?
Thank you!
-
You can change the post_type field for that post in the posts table using phpMyAdmin.
Backup your database before attempting.
Thank you MichaelH,
Which value do I have to enter in the post_type field?
I am looking at custom post types and they have “revision” in that field…
Thank you!
Let’s assume you have a custom post type of “book”.
You would change the post, where the post_type is post to book.
That revision record is not the actual post.
Related:
How do I determine a Post, Page, Category, Tag, Link, Link Category, or User ID?Thank you Michael, I was looking to another database by mistake.
Just another question: is it safe to change also the ID for ordering the custom post types?
Thank you!
No, I wouldn’t do that.
Note that you can use a hierarchical custom post type with the ‘page-attributes’ set so that you can get the Order under Page Attributes. See Function_Reference/register_post_type.
Or use the query post orderby argument.
Thank you MichaelH,
Once my posts have been converted to custom posts, I have enabled the page attributes for them:
add_action( 'init', 'create_my_post_types' ); function create_my_post_types() { $labels = array( ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'show_ui' => true, 'query_var' => true, 'menu_position' => 15, 'capability_type' => 'post', 'hierarchical' => true, 'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes'), 'rewrite' => array( 'slug' => 'projet', 'with_front' => true ), 'taxonomies' => array( 'typeprojet' ), 'permalink_epmask' => EP_PERMALINK, ); register_post_type('projets', $args); }
Then I am ordering them with the page attributes order.
But then when I try to navigate between these custom post types, why this order is not respected? I can only navigate between them ordered by ID.
This is the code in header.php (twentyten child theme):
<?php if ( is_singular('projet') ) { ?> <div id="nav-above" class="navigation"> <div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( ' ‹ ', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?></div> <div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( ' › ', 'Next post link', 'twentyten' ) . '</span>' ); ?></div> </div><!-- #nav-above --> <?php } ?>
Thank you.
The navgation is in the ‘order’ of the way the custom post type is sorted so you may need to use the query post orderby argument.
Thank you,
As I have the navigation in header.php, outside the main loop, I suppose I have to use the get_posts function, isn’t it?
<?php if ( is_single() || is_singular('projet') ) { ?> <?php get_posts('orderby=menu_order&order=ASC'); ?> <div id="nav-above" class="navigation"> <div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( ' ‹ ', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?></div> <div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( ' › ', 'Next post link', 'twentyten' ) . '</span>' ); ?></div> </div><!-- #nav-above --> <?php } ?>
But the navigation is completely the same. Also I have installed the postmash and reorder plugins, and my custom posts are already ordered, but I am not able to get the navigation work.
Any help regarding this will be really appreciated.
The ‘A page of posts’ example in the Pages article has a working pagination you might consider using. In that same article is an example of A Page of Posts for a Custom Post Type for use with the Twenty Ten theme (with working pagination).
Thank you MichaelH for all your assistance.
After reading carefully all the information, I have modified my template for my custom post type: single-projets.php placed in the folder of my twentyten child theme.
My custom post type is called “projet“.
Seems that I am doing something wrong because:
1. Title and content always shows the first one.
2. Pagination doesn’t work:
2a. previous_posts_link doesn’t return any value in any page.
2b. next_posts_link always returns a link to the current page with a parameter: domain.com/?projets=currentprojet&paged=2″Here is the code of my template (deleted several html code):
<?php /** * Template Name: Page of Projets * * @package WordPress * @subpackage Enzyme * @since 3.0.0 */ ?> <?php get_header(); ?> <?php $type = 'projets'; $meta_value_soustitre = get_post_meta ( $post->ID, 'Sous-Titre', true ); $meta_value_fiche = get_post_meta ( $post->ID, 'Fiche', true ); $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; $args = array( 'post_type' => $type, 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC', 'paged' => $paged, 'posts_per_page' => 1, 'caller_get_posts'=> 1 ); $temp = $wp_query; // assign orginal query to temp variable for later use $wp_query = null; $wp_query = new WP_Query($args); if( have_posts() ) : while ( $wp_query -> have_posts() ) : $wp_query -> the_post(); ?> <div id="container"> <?php the_title(); ?> <?php echo do_shortcode ( $meta_value_soustitre ); ?> </div><!-- .entry-meta --> <?php the_content(); ?> <?php echo do_shortcode ($meta_value_fiche); ?> </div><!-- #container --> <?php endwhile; // end of the loop. ?> <div class="navigation"> <div class="alignleft"><?php next_posts_link('? Older Entries') ?></div> <div class="alignright"><?php previous_posts_link('Newer Entries ?') ?></div> </div> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php get_search_form(); ?> <?php endif; $wp_query = $temp; //reset back to original query ?> <?php get_footer(); ?>
I’m really confused as seems nobody has the need of navigate between custom post types in a different order from chronological. I hope I’m closer to the solution… MichaelH, what’s wrong with my code?
Thank you very much for your help.
Using the twenty ten theme and creating a templage called projects.php and assigning to a page called Projects can you use the code in the Pages article. That code there worked fine for pagination with two posts per page.
Thank you, but I need to navigate between custom post types, not pages or normal posts.
I am not able to assign a template to a custom post type via admin area, and I suppose there is no need.
My custom post type is called “projets”, my template file is called “single-projets.php” and seems to work fine, except the navigation, title and content.
I think I have applied the code correctly but obviously not… because it is not working. Any advice on the published code?
Or maybe am I far away to the solution?
Well maybe someone else will have a different approach.
Thank you MichaelH,
I am confused: maybe I’m not explaining myself clearly but I can’t imagine that navigating between custom posts types ordered by the “order” attribute can’t be done in WP.
This is a basic feature for a CMS.
Hope the solution is simple or near to the code posted above.
Just please, if anybody can point me to any tutorial, plugin, professional programmer that can do this, let me know.
I really need this for ALL my WP sites. Thank you.
Ok, thanks to the Ambrosite Next/Previous Post Link Plus navigation between custom post types in several kinds of orders is possible.
Also, the Convert Post Types is useful for converting normal posts to custom post types.
Thank you MichaelH and all community for assistance.
- The topic ‘Convert posts to custom post types’ is closed to new replies.