Populate Custom Post Type from API with WP Cron Job

On the URU site we’ve been using the mz-mindbody-api plugin to pull in class, student, teacher details from Mindbody’s SOAP API.

But there were a few of significant limitations:

  • There wasn’t a way to “share” classes or teacher on social media
  • There wasn’t a way for search engines to index the details
  • The search function in the WordPress instance didn’t index the API results

So I decided to pull in an overview of all the classes (and events) using a Custom Post Type with a few Advanced Custom Fields (teacher name, level, class type).

It’s an overview because it will only have one of each unique class (designated by class name and teacher combined). Ultimately I’d like to take it a step further and display the details of each instance of each class (time, location, length) by adding a second dimension to the array of unique events.

Here I just wanted to document a few of the WordPress functions and methods that were helpful in developing and troubleshooting the plugin.

// BOF Output for debugging CPT
// Does out Post Type exist?
foreach ( get_post_types( '', 'names' ) as $post_type ) {
   echo '<p>' . $post_type . '</p>';
}


//let's look at our CPT:    
$type_obj = get_post_type_object($type);
    mz_pr($type_obj);

    // Even if the archive page is working, we can see if our CPT is returning anything
$type = 'yoga-event';
$args=array(
    'post_type' => $type,
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'ignore_sticky_posts'=> 1);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : 
    $my_query->the_post(); 
    ?>

    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to         <?php the_title_attribute(); ?>"><?php the_title(); ?> </a></p>

    <?php

    endwhile;

} // list of yoga-event items
    // See what the CPT thinks the post archive link is (if anything)
mz_pr(get_post_type_archive_link( 'yoga-event' ));

//Format arrays for display in development
if ( ! function_exists( 'mz_pr' ) ) {
    function mz_pr($message) {
        echo "<pre>";
        print_r($message);
        echo "</pre>";
    }
}
    //EOF Output for debugging CPT 

This was a really useful resource for getting the cron task going.

I also made a super-simple plugin to view the WP-cron task list.