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'll preface this by saying that I'm ok with CSS and Wordpress, but very new to PHP. You all seem to be an enormously smart bunch, and I was wondering if someone could help me out?

I am building a Wordpress site for a friend, and they are wanting to be able to post mixtapes. I have created two custom post types: 'mixtape' and 'track', and my friend wants to be able to post a 'mixtape' and then attach several child 'track' posts that will remain in the correct order and will only display on the parent post page.

I've been messing around with the WP Sub Page plugin ( http://takien.com/550/wordpress-plugin-wp-sub-post.php ) and trying to make it work with post types other than the default post type. Basically, it has the exact function that I'm looking for, but I need to be able to use it to link one post of the type 'track' to a parent post of the type 'mixtape'. Is this possible? I've included the plugin code below in case that helps.

I know this is a big ask, but I've been toiling away at this for three days now, to no avail. If anyone out there has the time to help me with this, I'd be so enormously appreciative! Just trying to learn how this all works!

Thanks.

Code:

<?php</p>
/*
Plugin Name: WP Sub Post
Description: You can make a post is a child of another post.
Author: Takien
Version: 0.1 Alpha
*/

////////////////////////////

class Walker_PostDropdown extends Walker {

    var $tree_type = 'post';

    var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');

    function start_el(&$output, $page, $depth, $args) {
        $pad = str_repeat(' ', $depth * 3);

        $output .= "\tID\"";
        if ( $page->ID == $args['selected'] )
            $output .= ' selected="selected"';
        $output .= '>';
        $title = esc_html($page->post_title);
        $output .= "$pad$title";
        $output .= "\n";
    }
}
/////////////
function &wp_get_posts($args = '') {

global $wpdb;

$defaults = array(
    'child_of' => 0, 'sort_order' => 'ASC',
    'sort_column' => 'post_title', 'hierarchical' => 1,
    'exclude' => '', 'include' => '',
    'meta_key' => '', 'meta_value' => '',
    'authors' => '', 'parent' => -1, 'exclude_tree' => '',
    'number' => '', 'offset' => 0
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$number = (int) $number;
$offset = (int) $offset;

$cache = array();
$key = md5( serialize( compact(array_keys($defaults)) ) );
if ( $cache = wp_cache_get( 'wp_get_posts', 'posts' ) ) {
    if ( is_array($cache) && isset( $cache[ $key ] ) ) {
        $pages = apply_filters('wp_get_posts', $cache[ $key ], $r );
        return $pages;
    }
}

if ( !is_array($cache) )
    $cache = array();

$inclusions = '';
if ( !empty($include) ) {
    $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include
    $parent = -1;
    $exclude = '';
    $meta_key = '';
    $meta_value = '';
    $hierarchical = false;
    $incpages = preg_split('/[\s,]+/',$include);
    if ( count($incpages) ) {
        foreach ( $incpages as $incpage ) {
            if (empty($inclusions))
                $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage);
            else
                $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage);
        }
    }
}
if (!empty($inclusions))
    $inclusions .= ')';

$exclusions = '';
if ( !empty($exclude) ) {
    $expages = preg_split('/[\s,]+/',$exclude);
    if ( count($expages) ) {
        foreach ( $expages as $expage ) {
            if (empty($exclusions))
                $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage);
            else
                $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage);
        }
    }
}
if (!empty($exclusions))
    $exclusions .= ')';

$author_query = '';
if (!empty($authors)) {
    $post_authors = preg_split('/[\s,]+/',$authors);

    if ( count($post_authors) ) {
        foreach ( $post_authors as $post_author ) {
            //Do we have an author id or an author login?
            if ( 0 == intval($post_author) ) {
                $post_author = get_userdatabylogin($post_author);
                if ( empty($post_author) )
                    continue;
                if ( empty($post_author->ID) )
                    continue;
                $post_author = $post_author->ID;
            }

            if ( '' == $author_query )
                $author_query = $wpdb->prepare(' post_author = %d ', $post_author);
            else
                $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author);
        }
        if ( '' != $author_query )
            $author_query = " AND ($author_query)";
    }
}

$join = '';
$where = "$exclusions $inclusions ";
if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) {
    $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";

    // meta_key and meta_value might be slashed
    $meta_key = stripslashes($meta_key);
    $meta_value = stripslashes($meta_value);
    if ( ! empty( $meta_key ) )
        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key);
    if ( ! empty( $meta_value ) )
        $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value);

}

if ( $parent >= 0 )
    $where .= $wpdb->prepare(' AND post_parent = %d ', $parent);

$query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'post' AND post_status = 'publish') $where ";
$query .= $author_query;
$query .= " ORDER BY " . $sort_column . " " . $sort_order ;

if ( !empty($number) )
    $query .= ' LIMIT ' . $offset . ',' . $number;

$pages = $wpdb->get_results($query);

if ( empty($pages) ) {
    $pages = apply_filters('wp_get_posts', array(), $r);
    return $pages;
}

// Sanitize before caching so it'll only get done once
$num_pages = count($pages);
for ($i = 0; $i < $num_pages; $i++) {
    $pages[$i] = sanitize_post($pages[$i], 'raw');
}

// Update cache.
update_page_cache($pages);

if ( $child_of || $hierarchical )
    $pages = & get_page_children($child_of, $pages);

if ( !empty($exclude_tree) ) {
    $exclude = (int) $exclude_tree;
    $children = get_page_children($exclude, $pages);
    $excludes = array();
    foreach ( $children as $child )
        $excludes[] = $child->ID;
    $excludes[] = $exclude;
    $num_pages = count($pages);
    for ( $i = 0; $i < $num_pages; $i++ ) {
        if ( in_array($pages[$i]->ID, $excludes) )
            unset($pages[$i]);
    }
}

$cache[ $key ] = $pages;
wp_cache_set( 'wp_get_posts', $cache, 'posts' );

$pages = apply_filters('wp_get_posts', $pages, $r);

return $pages;
}
//////////////

function walk_post_dropdown_tree() {
    $args = func_get_args();
    if ( empty($args[2]['walker']) ) // the user's options are the third parameter
        $walker = new Walker_PostDropdown;
    else
        $walker = $args[2]['walker'];

    return call_user_func_array(array(&$walker, 'walk'), $args);
}

function wp_dropdown_posts($args = '') {
$defaults = array(
    'depth' => 0, 'child_of' => 0,
    'selected' => 0, 'echo' => 1,
    'name' => 'page_id', 'show_option_none' => '', 'show_option_no_change' => '',
    'option_none_value' => ''
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

$pages = wp_get_posts($r);
$output = '';
$name = esc_attr($name);

if ( ! empty($pages) ) {
    $output = "
<select id=""$name\"" name="\"$name\"">
<option value="\"-1\"">$show_option_no_change</option>
<option value="\""">$show_option_none</option>

\n";
}

$output = apply_filters('wp_dropdown_pages', $output);

if ( $echo )
    echo $output;

return $output;
}
/////////////////////////////

/* Use the admin_menu action to define the custom boxes */
add_action('admin_menu', 'myplugin_add_custom_box');

/* Use the save_post action to do something with the data entered */
add_action('save_post', 'myplugin_save_postdata');

/* Adds a custom section to the "advanced" Post and Page edit screens */
function myplugin_add_custom_box() {

  if( function_exists( 'add_meta_box' )) {
    add_meta_box( 'myplugin_sectionid', __( 'WP Sub Posts', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'post', 'side','high' );
    //add_meta_box( $id,                  $title,                                      $callback,                  $page, $context, $priority );
    add_meta_box( 'myplugin_sectionid', __( 'WP Sub Posts', 'myplugin_textdomain' ), 'myplugin_inner_custom_box', 'page', 'advanced' );
   } else {
    add_action('dbx_post_advanced', 'myplugin_old_custom_box' );
    add_action('dbx_page_advanced', 'myplugin_old_custom_box' );
  }
}

function myplugin_inner_custom_box() {
echo '
<style type="text/css">
select#parent_id, select#parent_id option {
    width:250px;
}
</style>

';

echo '
<input id="myplugin_noncename" name="myplugin_noncename" type="hidden" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';  ?>
<h5><?php _e('Parent') ?></h5>

<label for="post_parent">Please select the parent of this post</label>

<?php 

$currentid = $_GET['post'];
wp_dropdown_posts(array('exclude_tree'             => $currentid,
                            'selected'             => $post->post_parent,
                            'name'                 => 'parent_id',
                            'show_option_none'     => __('Main Post (no parent)'),
                            'sort_column'        => 'menu_order, post_title'));
 }

/* Prints the edit form for pre-WordPress 2.5 post/page */
function myplugin_old_custom_box() {

  echo '
<div class="dbx-b-ox-wrapper">' . "\n";
  echo '
<fieldset id="myplugin_fieldsetid" class="dbx-box">' . "\n";
  echo '
<div class="dbx-h-andle-wrapper">
<h3 class="dbx-handle">' .
        __( 'WP Sub Posts', 'myplugin_textdomain' ) . "</h3>
</div>

";   

  echo '
<div class="dbx-c-ontent-wrapper">
<div class="dbx-content">';

  // output editing form

  myplugin_inner_custom_box();

  // end wrapper

  echo "</div>
</div>
</fieldset>
</div>

\n";
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times

  if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
    return $post_id;
  }

  // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
  // to do anything
  if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
    return $post_id;

  // Check permissions
  if ( 'page' == $_POST['post_type'] ) {
    if ( !current_user_can( 'edit_page', $post_id ) )
      return $post_id;
  } else {
    if ( !current_user_can( 'edit_post', $post_id ) )
      return $post_id;
  }

  // OK, we're authenticated: we need to find and save the data

  $mydata = $_POST['parent_id'];

   return $mydata;
}

add_filter('posts_where','where_no_parent');
add_filter ('the_content','subpost_template', 1, 2 );

function subpost_template($content) {

    if(is_singular) {
    remove_filter('posts_where','where_no_parent');
    }
    echo $content;

    $idnya = get_the_ID();
    $child = new WP_Query("post_type=post&post_parent=".$idnya."");

    while($child->have_posts()) : $child->the_post();
    echo '
<blockquote>

';
    echo '
<h3>';
    the_title();

    echo '</h3>

';
    $childid =  get_the_ID();
    the_content();

    edit_post_link('Edit','','',$childid);
    echo '</blockquote>

';

    endwhile;
    wp_reset_query();
}

function where_no_parent($where) {
    global $wpdb;
     $where .= " AND post_parent  = 0";
     return $where;
}
?>

?>
share|improve this question

2 Answers 2

Haven't tried this, but if you look at the code, there's a query where the posts are retrieved from the database:

$query = "SELECT * FROM $wpdb->posts $join WHERE (post_type = 'post' AND post_status = 'publish') $where ";

As you can see, it's looking for post_type = 'post'. So this will only get the default 'post' type posts. You can modify it yourself to put 'track' or 'mixtape' there, but I would also recommend contacting the original developer and ask him/her to make the post_type a parameter in the plugin.

Hope this helps!

share|improve this answer

Instead of trying to modify the plugin you've found, you can also use a plugin called Sub Posts. Sub Posts will enable you to create relations between any post types you like and start creating links between them in minutes. You can display these created links in the widget or with the shortcode included in this plugin. You can find more information about Sub Posts at www.subposts.com

share|improve this answer

Your Answer

 
discard

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