Bundling up content by adding posts to a custom post type

  • Unknown's avatar

    Hello,

    I am trying to create a custom post type called “Bundle”. From the custom post type the user is able to select which content they wish to be included in the bundle, be it posts or other custom post types. I’d like them to be displayed kind of the same way as this

    http://wp.tutsplus.com/tutorials/display-style-related-posts-for-your-wordpress-site/

    Right now my problem is diplaying it the right way, and i cannot get it to save the content i’ve selected through the check boxes.

    add_action('add_meta_boxes', 'bundle_content_meta_box_INIT');
    function bundle_content_meta_box_INIT() {
    add_meta_box('bundle_content', 'Add Content to the bundle', 'add_content_box', 'bundle','normal');
    }
    function add_content_box(){
    global $post;

    // => add_content_box()
    $checks_array = get_post_meta($post->ID, "checks", true);

    $checks = explode (",",$checks_array);

    $query = new WP_Query( array ('post_type' => array ('post','page','videos')));

    $rows = array("post" => array(), "page" => array(), "videos" => array());

    while ($query->have_posts()) : $query->the_post();
    $check_text='';
    if(in_array($post->ID, $checks)) {
    $check_text='checked';
    }
    $row_html = "<input type='checkbox' $chk_text name='chks[]' value='" . $post->ID . "' id='chk-" . $post->ID . "'> ". $post->post_title;

    $rows[$post->post_type][] = $row_html;
    endwhile;

    foreach($rows as $post_type => $post_type_rows){
    echo "<h2>".ucfirst($post_type)."</h2>";
    echo implode("
    ", $post_type_rows);
    }

    echo '<input type="hidden" name="bundle_content_meta_box_nonce" id="" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    }

    add_action('save_post', 'bundle_content_save_data');

    // saving meta box data
    function bundle_content_save_data($post_id) {
    global $post;

    // => bundle_content_save_data
    global $post;

    if ( !wp_verify_nonce( $_POST['bundle_content_meta_box_nonce'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( !current_user_can( 'edit_post', $post->ID ))
    return $post->ID;

    // OK, we're authenticated: we need to find and save the data
    // We'll put it into an array to make it easier to loop though.

    $meta_box["checks"] = $_POST["checks"];

    // Add values of $events_meta as custom fields

    foreach ($meta_box as $key => $value) { // Cycle through the $events_meta array!
    if( $post->post_type == 'revision' ) return; // Don't store custom data twice
    $value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
    update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn't have a value
    add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }
    }

  • Unknown's avatar

    You did not specify a blog address or reason for posting when you created this topic.

    This support forum is for blogs hosted at WordPress.com. If your question is about a self-hosted WordPress blog then you’ll find help at the WordPress.org forums.

    If you don’t understand the difference between WordPress.com and WordPress.org, you may find this information helpful.

    If you forgot to include a link to your blog, you can reply and include it below. It’ll help people to answer your question.

    This is an automated message.

  • Unknown's avatar

    I’m sorry to report that you have accidentally posted to the wrong support forum. We cannot help you here at WordPress.com with that site as it’s not being hosted by WordPress.com and we run on different software. This is the correct forum for your software http://wordpress.ORG/support/
    WordPress.com vs WordPress.org: The Differences
    http://support.wordpress.com/com-vs-org/
    Best wishes with your site.

  • The topic ‘Bundling up content by adding posts to a custom post type’ is closed to new replies.