. copyright (c) 2005 Scott Merrill (skippy@skippy.net) Copyright 2006, 2007 Travis Snoozy (ai2097@users.sourceforge.net) Released under the terms of the GNU GPL v2 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ //////////////////////////////////////// function get_all_in_series($series = '') { global $post, $wpdb; if ('' == $series) { return; } // no sense looking for blank data $author = $post->post_author; $now = current_time('mysql'); $arrPost_IDs = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta INNER JOIN $wpdb->posts ON $wpdb->postmeta.post_id=$wpdb->posts.ID WHERE meta_key='_series_name' AND meta_value='$series' AND $wpdb->posts.post_author = '$author' AND $wpdb->posts.post_date < '$now' AND($wpdb->posts.post_status='publish' OR $wpdb->posts.post_status='static')"); if (0 == count($arrPost_IDs)) { return; } $post_IDs = implode(',', $arrPost_IDs); $ids = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE post_id IN ($post_IDs) AND meta_key='_series_order' ORDER BY CAST(meta_value AS SIGNED) ASC"); // now prepare an array of empty items ordered by post ID $this_series = array(); foreach ($ids as $this_id) { $this_series[$this_id] = ''; } $s = implode(',', $ids); $series_posts = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE ID IN ($s)", OBJECT); // now populate the array of posts with data foreach ($series_posts as $s_post) { $this_series[intval($s_post->ID)] = $s_post->post_title; } return $this_series; } /////////////////////////////////// function all_in_series($sep=',', $before='« Read the whole series: ', $after=' »') { global $post; $series = strtolower(get_post_meta($post->ID, '_series_name', true)); if ('' == $series) { return null; } $series_posts = get_all_in_series($series); $output = ''; $count = 1; foreach ($series_posts as $s_id => $s_title) { if ('' != $output) { $output .= $sep; } if ($s_id == $post->ID) { $output .= $count; } else { $output .= "$count"; } $count++; } $output = $before . $output . $after; echo $output; } /////////////////////////// function get_all_series($order = 'name', $published = true) { global $wpdb; $inner_order = ''; $outer_order = ''; if ('id' == $order) { $inner_order='DESC'; $outer_order='CAST(d.post_id AS SIGNED) DESC'; } else { $inner_order='ASC'; $outer_order='NULL'; } $filter_unpublished=""; if($published !== false) { $filter_unpublished= " JOIN (SELECT ID FROM $wpdb->posts WHERE post_status='publish') c ON c.ID=a.post_id"; } // FIXME: Ordering by post_id probably doesn't work as expected. $result = $wpdb->get_results( "SELECT d.post_id,d.meta_value FROM (SELECT a.post_id,a.meta_value FROM (SELECT post_id,meta_value FROM $wpdb->postmeta WHERE meta_key='_series_name') a JOIN (SELECT post_id,meta_value FROM $wpdb->postmeta WHERE meta_key='_series_order') b ON a.post_id=b.post_id $filter_unpublished ORDER BY a.meta_value,CAST(b.meta_value AS SIGNED) $inner_order) d GROUP BY d.meta_value ORDER BY $outer_order"); if ( (! $result) || ( count($result) == 0 ) ) { return FALSE; } $all = Array(); foreach ($result as $r) { $all["{$r->post_id}"] = $r->meta_value; } return $all; } /////////////////////// function all_series($before = '
    ', $after = '
', $order = 'name') { $all_series = get_all_series($order); if (! $all_series) { return; } $output = $before; foreach ($all_series as $id => $title) { $output .= "
  • $title
  • "; } $output .= $after; echo $output; } ///////////////////////////////////// function series_table_of_contents($class='', $before = '
      ', $after = '
    ') { global $post; $series = strtolower(get_post_meta($post->ID, '_series_name', true)); if ('' == $series) { return null; } if (! is_single() ) { return; } $series_posts = get_all_in_series($series); $output = $before; foreach ($series_posts as $s_id => $s_title) { $output .= 'ID) { $output .= ""; } $output .= $s_title; if ($s_id != $post->ID) { $output .= ''; } $output .= ''; } $output .= $after; echo $output; } //////////////////////////////// function get_previous_in_series($single_only = true) { global $post, $wpdb; if(! is_single() && $single_only) { return null; } $series = strtolower(get_post_meta($post->ID, '_series_name', true)); if (empty($series)) { return null; } $position = get_post_meta($post->ID, '_series_order', true); $s = implode(',', $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_series_name' AND meta_value='$series'")); $author = $post->post_author; $now = current_time('mysql'); $previous_id = $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta INNER JOIN $wpdb->posts ON $wpdb->postmeta.post_id=$wpdb->posts.ID WHERE post_id IN ($s) AND meta_key='_series_order' AND CAST(meta_value AS SIGNED) < $position AND $wpdb->posts.post_author = '$author' AND $wpdb->posts.post_date < '$now' AND ($wpdb->posts.post_status='publish' OR $wpdb->posts.post_status='static') ORDER BY CAST(meta_value AS SIGNED) DESC LIMIT 1"); if ($previous_id) { return @$wpdb->get_row( "SELECT ID, post_title FROM $wpdb->posts WHERE ID = $previous_id LIMIT 1"); } else { return null; } } ///////////////////////////// function get_next_in_series($single_only = true) { global $post, $wpdb; if(! is_single() && $single_only) { return null; } $series = strtolower(get_post_meta($post->ID, '_series_name', true)); if (empty($series)) { return null; } $position = get_post_meta($post->ID, '_series_order', true); $s = implode(',', $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_series_name' AND meta_value='$series'")); $author = $post->post_author; $now = current_time('mysql'); $next_id = $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta INNER JOIN $wpdb->posts ON $wpdb->postmeta.post_id=$wpdb->posts.ID WHERE post_id IN ($s) AND meta_key='_series_order' AND CAST(meta_value AS SIGNED) > $position AND $wpdb->posts.post_author = '$author' AND $wpdb->posts.post_date < '$now' AND ($wpdb->posts.post_status='publish' OR $wpdb->posts.post_status='static') ORDER BY CAST(meta_value AS SIGNED) ASC LIMIT 1"); if ($next_id) { return @$wpdb->get_row( "SELECT ID, post_title FROM $wpdb->posts WHERE ID = $next_id LIMIT 1"); } else { return null; } } ///////////////////////////// function previous_in_series($format='« %link', $link='%title') { $p_post = get_previous_in_series(); if(! $p_post) { return; } $title = apply_filters('the_title', $p_post->post_title, $p_post); $string = ''; $link = str_replace('%title', $title, $link); $link = $string . $link . ''; $format = str_replace('%link', $link, $format); echo $format; } ///////////////////////// function next_in_series($format='%link »', $link='%title') { $n_post = get_next_in_series(); if(! $n_post) { return; } $title = apply_filters('the_title', $n_post->post_title, $n_post); $string = ''; $link = str_replace('%title', $title, $link); $link = $string . $link . ''; $format = str_replace('%link', $link, $format); echo $format; } ///////////////////////// function display_manage_posts_in_series_column($colname, $id) { if(! $colname || $colname != "in_series") { return; } echo get_post_meta($id, '_series_name', true); } add_action('manage_posts_custom_column', 'display_manage_posts_in_series_column', 10, 2); ///////////////////////// function add_manage_posts_in_series_column($posts_columns) { $posts_columns['in_series'] = __('Series'); return $posts_columns; } add_filter('manage_posts_columns', 'add_manage_posts_in_series_column'); ///////////////////////// function add_write_post_in_series_sidebar() { global $post; $series = get_post_meta($post->ID, '_series_name', true); ?>

    :

    $series"; } ?>
    query( "DELETE FROM $wpdb->postmeta WHERE post_id='$id' AND (meta_key='_series_name' OR meta_key='_series_order')"); //XXX: This means you should NOT have a series called 'delete'. if($series == 'delete') { return; } $sort = "DESC"; $offset = 1; if($place != 'append') { $sort = "ASC"; $offset = -1; } //SQL query to get largest/smallest _series_order value. $series_order = intval($wpdb->get_var( "SELECT sorder.meta_value FROM $wpdb->postmeta AS sorder INNER JOIN $wpdb->postmeta AS sname ON sorder.post_id=sname.post_id WHERE sname.meta_key='_series_name' AND sorder.meta_key='_series_order' ORDER BY CAST(sorder.meta_value AS SIGNED) $sort LIMIT 1")); $series_order += $offset; // Don't allow a series order of 0; skip to 1 or -1. if($series_order == 0) { $series_order += $offset; } //SQL query to insert _series_name and _series_order values. $wpdb->query( "INSERT INTO $wpdb->postmeta (post_id,meta_key,meta_value,meta_id) VALUES ($id, '_series_name', '$series', DEFAULT), ($id, '_series_order', '$series_order', DEFAULT)"); } add_action('save_post', 'process_save_in_series'); ///////////////////////// // Convert old in-series metadata to the new format. function in_series_pre_2_0_metadata_convert() { global $wpdb; $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key='_series_name' WHERE meta_key='series_name'"); $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key='_series_order' WHERE meta_key='series_order'"); } add_action('activate_in-series.php', 'in_series_pre_2_0_metadata_convert'); ?>