• zipadee

    (@zipadee)


    I have a custom post type called Testimonies. What I’m trying to do is allow a user to create a new Testimony and then select from a list of checkboxes which page(s) on the site he wants this testimony to display on.

    I have managed to create the checkboxes to list all the site pages and code is below:

    add_action("admin_init", "admin_init3");
    
    function admin_init3(){
    add_meta_box("credits_meta", "Choose on which pages to display",     "myplugin_inner_custom_box", "testimonies", "normal", "low");
    }
    
    function myplugin_inner_custom_box( $post ) {
    // we store data as an array, we need to unserialize it
    $checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );
    
    // Nonce to verify intention later
    wp_nonce_field( 'save_quote_meta', 'custom_nonce' ); 
    
    $pages = get_pages();
    foreach ( $pages as $page ) { ?>
        <input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/> <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></label> <br>
    <?php
    }
    }
    //save the meta box action
    add_action( 'save_post', 'myplugin_meta_save', 10, 2 );
    
    //save the meta box
    function myplugin_meta_save($post_id, $post)
    {
    if ( isset($_POST['checkfield']) ) { // if we get new data
    
        update_post_meta($post_id, "checkfield", $_POST['checkfield'] );
    
    }
    }

    However, I’m having difficulty with the next step. My idea is to put the checkbox values in a variable, then check whether the current page id matches one of these values. If it does, I’ll display the custom post. This is all a bit hazy though, and I’m not sure if I’m going about it the right way.

    Currently, I’m struggling to pass the checkbox values into a variable and echo these out. I have the below code but it’s just echoing array, array, array, array.

    <?php  $checkboxes=get_post_meta($post->ID, "checklist", false);
    if ($checkboxes) {
    foreach($checkboxes as $checkbox) {
    echo  $checkbox;
    }
    } ?>

    Any help would be much appreciated.

    Many thanks.

Viewing 1 replies (of 1 total)
  • Casey Driscoll

    (@caseypatrickdriscoll)

    Hi zipadee,

    I’d have to take more time to look through more of your code to get a better grasp on this.

    I do think however that the Posts-2-Posts plugin will give you exactly what you want. You can easily define to, from and reciprocal relationship among cpt’s and then easily display them in the loop.

    http://wordpress.org/plugins/posts-to-posts/

    I’ve only dabbled with it a little bit, but it seems to me you can easily create a ‘has many’ relationship from pages to testimonies.

    <?php
    function testimony_connection_types() {
        p2p_register_connection_type( array(
            'name' => 'testimonies_to_pages',
            'from' => 'testimony',
            'to' => 'page',
            'cardinality' => 'one-to-many',
        ) );
    }
    add_action( 'p2p_init', 'testimony_connection_types' );
    ?>

    Code taken from: https://github.com/scribu/wp-posts-to-posts/wiki

    Not sure the code I give is exactly accurate, more of an idea. Let me know if this approach doesn’t work.

    Cheers!

Viewing 1 replies (of 1 total)
  • The topic ‘Selecting which pages to display custom post type on using checkbox’ is closed to new replies.