0

I am trying to write a plugin with a CPT and have a shortcode to display the data. I can get 'the_title()' to work and display the title of the post type but using 'get_post_meta()' I can't seem to get it to work. Here is my code and any help is greatly appreciated. Thanks!

function ClientDirectoriesCPT() {
    add_action( 'init', array( $this, 'register_custom_post_type' ) );
    add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) );
    add_action( 'save_post', array( $this, 'save_meta_boxes' ) );
    add_shortcode( 'client-directory-page', array( $this, 'build_client_directory_page' ) );
}


function build_client_directory_page( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'client',
        'posts_per_page' => 10,
        'orderby' => 'meta_value',
        'order' => 'ASC'
    ) );
    extract(shortcode_atts(array(
        'id' => null,
    ), $atts));

    if ( $query->have_posts() ) { ?>
        <?php while ( $query->have_posts() ) : $query->the_post();

        $client_name = get_post_meta($post->ID, 'client_name', true); ?>

            <div class="row">
                <div class="col-sm-6"><?= the_title(); ?></div>
                <div class="col-sm-6"><?= $client_name ?></div>
            </div>
        <?php endwhile;
        wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

/**
* Registers a Meta Box on our Client Directory Custom Post Type, called 'Client Details'
*/
function register_meta_boxes() {
    add_meta_box( 'client-details', 'Client Details', array( $this, 'output_meta_box' ), 'client', 'normal', 'high' );
}

/**
* Output a Client Details meta box
*
* @param WP_Post $post WordPress Post object
*/
function output_meta_box( $post ) {
    // 2nd with Name, address, website and description

    $client_name = get_post_meta( $post->ID, '_client_name', true );
    $client_address = get_post_meta( $post->ID, '_client_address', true );
    $client_url = get_post_meta( $post->ID, '_client_url', true );
    $client_description = get_post_meta( $post->ID, '_client_description', true );

    // Add a nonce field so we can check for it later.
    wp_nonce_field('save_client', 'clients_nonce' );

    // Output label and field
    echo ('<p><label for="client_name">' . __( 'Name', 'cd-crm' ) . '</label>' );
    echo ('<input type="text" name="client_name" id="client_name" value="'.esc_attr( $client_name ).'" /></p>' );

    echo ('<p><label for="client_address">' . __( 'Address', 'cd-crm' ) . '</label>' );
    echo ('<input type="text" name="client_address" id="client_address" value="'.esc_attr( $client_address ).'" /></p>' );

    echo ('<p><label for="client_url">' . __( 'URL', 'cd-crm' ) . '</label>' );
    echo ('<input type="text" name="client_url" id="client_url" value="'.esc_attr( $client_url ).'" /></p>' );

    echo ('<p><label for="client_description">' . __( 'Description', 'cd-crm' ) . '</label>' );
    echo ('<textarea rows="5" cols="30" id="client_description" name="client_description">' . esc_html( $client_description ) . '</textarea>' );
}

/**
* Saves the meta box field data
*
* @param int $post_id Post ID
*/
function save_meta_boxes( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST['clients_nonce'] ) ) {
        return $post_id;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['clients_nonce'], 'save_client' ) ) {
        return $post_id;
    }

    // Check this is the Contact Custom Post Type
    if ( 'client' != $_POST['post_type'] ) {
        return $post_id;
    }

    // Check the logged in user has permission to edit this post
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return $post_id;
    }

    // OK to save meta data
    $client_name = sanitize_text_field( $_POST['client_name'] );
    update_post_meta( $post_id, '_client_name', $client_name );

    $client_address = sanitize_text_field( $_POST['client_address'] );
    update_post_meta( $post_id, '_client_address', $client_address );

    $client_url = sanitize_text_field( $_POST['client_url'] );
    update_post_meta( $post_id, '_client_url', $client_url );

    // $client_description = sanitize_text_field( $_POST['client_description'] );
    // update_post_meta( $post_id, '_client_description', $client_description );

    // Save the textarea
    if ( isset( $_POST['client_description'] ) ) {

        // WP's default allowed tags
        global $allowedtags;

        // allow iframe only in this instance
        $iframe = array( 'iframe' => array(
                            'src' => array (),
                            'width' => array (),
                            'height' => array (),
                            'frameborder' => array(),
                            'allowFullScreen' => array() // add any other attributes you wish to allow
                             ) );

        $allowed_html = array_merge( $allowedtags, $iframe );

        // Sanitize user input.
        $my_data = wp_kses( $_POST['client_description'], $allowed_html );

        //$my_data = $_POST['client_description'];

        // Update the meta field in the database.
        update_post_meta( $post_id, '_client_description', $my_data );
    }

}'
  • 1
    You are using shorthand PHP tags, like <?= /*some stuff*/ ?> they won't work - see WordPress Coding Standards. Besides you are not echo'ing the variable $client_name. – Nicolai Sep 30 '14 at 19:05
  • If I change it out to this <div class="col-sm-6"><?php echo $client_name ?></div> seems it's not working either – tjoenz Sep 30 '14 at 19:13
  • 1
    what happens if you use get_the_ID() instead of $post->ID? – Milo Sep 30 '14 at 19:14
  • @Milo when I change it to $client_name = get_post_meta( get_the_ID(), 'client_name', true ); ?> nothing displays either – tjoenz Sep 30 '14 at 19:17
  • 1
    Inside the the metabox function the post meta name is _client_name, so with an additional underscore at the front. – Nicolai Sep 30 '14 at 19:37
1

There are several minor problems with your code:

Firstly, you are using shorthand PHP tags, like <?= /*some stuff*/ ?> they won't work - see WordPress Coding Standards - No Shorthand PHP Tags.

Secondly, you are not echo'ing the variable $client_name.

Last but not least, you are saving your post meta with the name _client_name, with an additional underscore at the front, but made a typo while trying to get it, the underscore is missing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.