admin管理员组

文章数量:1328893

I have 2 custom post types: agenda and doel. At each agenda post, I want to choose a doel post to attach it to, and the same with normal posts. So I want a connection between posts, agenda, en doel.

I found this plugin called posts2posts from scribu, it does attach the posts types to each other, but I can not find out how and if it is possible to connect certain posts from one post type to certain posts from an other post type.

The ideal solution would be to have a dropdown in one post type, that shows all the posts from an other one, and you can select one. In example, when editing a post from agenda, I can choose a post from doel to attach it to.

And later on I want to loop that.

Does someone know how this can be done easily, or recommend a good plugin?

Sorry for my bad explanation, hope it is kind of clear.

I have 2 custom post types: agenda and doel. At each agenda post, I want to choose a doel post to attach it to, and the same with normal posts. So I want a connection between posts, agenda, en doel.

I found this plugin called posts2posts from scribu, it does attach the posts types to each other, but I can not find out how and if it is possible to connect certain posts from one post type to certain posts from an other post type.

The ideal solution would be to have a dropdown in one post type, that shows all the posts from an other one, and you can select one. In example, when editing a post from agenda, I can choose a post from doel to attach it to.

And later on I want to loop that.

Does someone know how this can be done easily, or recommend a good plugin?

Sorry for my bad explanation, hope it is kind of clear.

Share Improve this question asked Jan 15, 2014 at 15:50 MaartjeMaartje 5471 gold badge9 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

Plugin Recommendations are considered off-topic here. One solution could be to add a metabox to your post like so:

Add to Functions.php

/** Add Post Metabox **/
function add_custom_meta_box() {
    add_meta_box(
        'pick_post', // $id
        'Pick Post', // $title 
        'show_custom_meta_box_pick_post', // $callback
        'agenda', // $page
        'side', // $context
        'high'); // $priority
}
add_action('add_meta_boxes', 'add_custom_meta_box');

/** Post Metabox Callback (show some stuff in box) **/
function show_custom_meta_box_pick_post() {
    $doelArr = get_posts( array( 'post_type' => 'doel', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1 ) );
    $meta_doel = get_post_meta($post->ID, '_doellist', true);
?>
    <input type="hidden" name="info_meta_box_nonce" value="<?php echo wp_create_nonce(basename(__FILE__)); ?>" />
    <strong>Associated Doel</strong>
        <br />
    <select id="selectdoel" name="_doellist">
        <option value="">Select Doel</option>
    <?php foreach($doelArr as $doel) : setup_postdata( $doel ) ?>
        <?php if($doel->ID == $meta_doel) : ?>
            <option value="<?php echo $doel->ID; ?>" selected="selected"><?php echo $doel->post_title; ?></option>
        <?php else : ?>
            <option value="<?php echo $doel->ID; ?>"><?php echo $doel->post_title; ?></option>
        <?php endif; ?>
    <?php endforeach; ?>
    </select>
}

/** Save Our Meta-data **/
function save_custom_meta($post_id) {
    global $post;

    // check autosave
    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (!current_user_can('edit_post', $post_id)))
        return $post_id;

    if($post->post_type == 'agenda')
    {
        // Doel Selection List Check
        if(isset($_POST['_doellist']) && $_POST['_doellist'] != '')
            update_post_meta($post_id, '_doellist', strip_tags($_POST['_doellist']));
        else
            delete_post_meta($post_id, '_doellist');
    }
}

What this does is create a small metabox with a dropdown of Doel posts - then saved the Doel Post ID as a meta value. You can get that meta-value (Doel Post ID) at any time by passing in the Agenda ID to get_post_meta() function.

$meta_doel = get_post_meta($post->ID, '_doellist', true);

Then you could get the entire Doel post like so:

$doel_post = get_post($meta_doel);

本文标签: attach one post from one post type to another post from another post type