Archive for the ‘Custom Post Types’ Category

Convert one post type to another another

If you can’t tell yet, we’re a huge fan of custom post types in our theme development and blog tutorials.  They allow us to create feature rich admin areas while keeping with the intuitiveness that is WordPress; so, of course, we dig em!  Have you ever created a custom post type, but wanted to convert a bunch of already written posts into that new post type?  To accomplish this task, we’ve used a Post Type Switcher in the past, however, as of WordPress 3.1 that plugin no longer works so we needed to get down and dirty in the database for this one.  Here is how you do it:

  1. Backup your database just in case you need to roll back for some reason, we’ve never had a problem running the SQL below, but its generally a good idea to back up the DB when performing any action like this.
  2. Run the SQL below in phpMyAdmin, tweaking it to your needs first.
UPDATE wp_posts p
JOIN wp_term_relationships tr ON (p.ID = tr.object_id)
JOIN wp_term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
JOIN wp_terms t ON (tt.term_id = t.term_id)
SET post_type = 'event'
WHERE p.post_type = 'post'
AND p.post_status = 'publish'
AND tt.taxonomy = 'category'
AND t.name = 'Events'

The code above takes posts in the “post” post type that are “publish”ed and in the category “Events” and converts them to the post type “event”. Yes, we’re using The Events Calendar as an example yet again.  Now your events that used to be grouped in with all your other posts now have a neat new home under their own Event post type menu in the admin, making tutorials for clients that much easier!

Custom Post Type Admin Icons

Let’s face it, its pretty awesome having the ability to add a custom icon to your admin menus for custom post types. Here is a bit of code we added to the function.php file of our theme for a recent project.  All you need to do is drop your custom icons in the images directory of you theme and tweak the code below. “Newsletters” and “Events” are the names of our 2 custom post types for this project.

add_action( 'admin_head', 'cpt_icons' );
function cpt_icons() {
    ?>
    <style type="text/css" media="screen">
		#menu-posts-newsletters .wp-menu-image {
            background: url(<?php bloginfo('template_url') ?>/images/mail.png) no-repeat 6px -17px !important;
        }
		#menu-posts-events .wp-menu-image {
            background: url(<?php bloginfo('template_url') ?>/images/calendar-blue.png) no-repeat 6px -17px !important;
        }
		#menu-posts-newsletters:hover .wp-menu-image, #menu-posts-newsletters.wp-has-current-submenu .wp-menu-image,
		#menu-posts-events:hover .wp-menu-image, #menu-posts-events.wp-has-current-submenu .wp-menu-image {
            background-position: 6px 7px!important;
        }
    </style>
<?php }

You can find a sweet collection of icons and further direction at RandyJensenOnline.com.

Custom Post Type RSS Feed

Here is a neat little trick to display all posts from a custom post type via feed in WordPress. This comes in handy if you’re looking to pull posts from a certain post type into an external site using something like the Google RSS Feed Displayer.

http://example.com/feed/?post_type=yourposttype

The Events Calendar and Custom Post Types

The Events Calendar plugin is outstanding for tracking events within WordPress sites and blogs. The integration of templates into your own theme means you don’t risk overwriting updates each time you upgrade the plugin. This is probably the strongest event calendar plugin we’ve used, but that doesn’t mean there aren’t some tweaks that could be made. Unfortunately, all events are listed as regular posts in WordPress; this works fine, but if you’re a neat-freak like we are, you like to have everything in its own place using custom post types. Because The Events Calendar doesn’t support custom post types (yet), we have to force it a little. It’s actually pretty easy and only take a little bit of code to complete, check it out below.

On line 1029 of this file:
/wp-content/plugins/the-events-calendar/the-events-calendar.class.php

Replace this:

public function addEventBox( ) {
add_meta_box( 'Event Details', __( 'The Events Calendar', 'Events_textdomain' ),
array( $this, 'EventsChooserBox' ), 'post', 'normal', 'high' );
}

With this:

public function addEventBox( ) {
add_meta_box(
'Event Details', __( 'The Events Calendar', 'Events_textdomain' ),
array( $this, 'EventsChooserBox' ), 'post', 'normal', 'high' );
add_meta_box( 'Event Details', __( 'The Events Calendar', 'Events_textdomain' ),
array( $this, 'EventsChooserBox' ), 'events', 'normal', 'high' );
}

The word “events” on line 6 is the name of your custom post type. We use “events” because, well, they are events.

Now that the events are showing in your custom post types, you’ll need to edit the events templates, these are kept in the “events” directory within your theme. All you need to do is tweak the query to pull the custom post type, so add the following line of code right before the loop starts:

<?php query_posts('post_type=events&category_name=events&eventDisplay=upcoming'); ?>