Bernard Posniak
Bernard Posniak

Reputation: 1

Creating Custom Fields and Meta Boxes

The code I am working on is returning is not returning the value for the telephone number

I am trying to create a custom post for business locations

I am missing something, and I cant see it.

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

function locations_post(){
    register_post_type('locations', array(
        'labels' => array(
            'name'=>__('Locations'),
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
        ),
        'taxonomies' => array(
            'post_tag',
            'category',
        )
    ));
    register_taxonomy_for_object_type('category','locations');
    register_taxonomy_for_object_type('post_tag','locations');
}
add_action('init','locations_post');


function locations_meta_box(){
    add_meta_box(
        'locations_fields_meta_box',//$id
        'Locations',//$title
        'show_locations_fields_meta_box',//$callback
        'locations',//$screen
        'normal',//$context
        'high'//$priority
    );
}
add_action('add_meta_boxes','locations_meta_box');


function show_locations_fields_meta_box(){
    global $post;
    $locations = get_post_meta($post->ID,'locations_fields',true);
    ?>
    <input type="hidden" name="locations_meta_box_nonce"value="<?php echo wp_create_nonce(basename(__FILE__)); ?> ">
    <p>
        <labelfor="locations_fields[text]">Phone</label><br>
        <input type="text" name="locations_fields[telephone]" id="locations_fields[telephone]" class="regular-text" value="<?php echo $locations['telephone'];?>">
    </p>
    <?php

}
add_action('add_meta_boxes','show_locations_fields_meta_box');

function save_locations_fields_meta($post_id) {
    if(!wp_verify_nonce($_POST['locations_meta_box_nonce'],basename(__FILE__))) {
        return$post_id;
    }
    if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) {
        return$post_id;
    }
    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;
        }
    }
    $old = get_post_meta($post_id,'locations_fields',true);
    $new = $_POST['locations_fields'];
    if($new&&$new!==$old) {
        update_post_meta($post_id,'locations_fields',$new);
    } elseif(''===$new && $old) {
        delete_post_meta( $post_id,'locations_fields',$old );
    }
}
add_action('save_post','save_locations_fields_meta');

Upvotes: 0

Views: 88

Answers (2)

yrubin07
yrubin07

Reputation: 13

In this function:

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

there appears to be a "s" missing on this line $locations = get_post_meta($post->ID,'location_fields',true); should be $locations = get_post_meta($post->ID,'locations_fields',true); also for $post->ID make sure the value is valid if not might want to try this: https://developer.wordpress.org/reference/functions/get_the_id/

Upvotes: 0

Vel
Vel

Reputation: 9342

Try this code. I have commented this line //add_action('add_meta_boxes','show_locations_fields_meta_box');. This will add 2 meta boxes.

And also change in save post code.

function locations_footer() {
    $args = array('post_type'=>'locations',);
    $location_loop = new WP_Query($args);
    if($location_loop->have_posts()):
        while($location_loop->have_posts()):
            $location_loop->the_post();
            $locations = get_post_meta($post->ID,'location_fields',true);
            $telephone = $locations['telephone'];
            $title = get_the_title();
            $content = get_the_content();
            echo'Title:'.$title.'<br>Telephone:'.$telephone.'<br>Content:'.$content.'';
        endwhile;
    endif;
    wp_reset_postdata();
}
add_action('wp_footer','locations_footer');

function locations_post(){
    register_post_type('locations', array(
        'labels' => array(
            'name'=>__('Locations'),
        ),
        'public' => true,
        'hierarchical' => true,
        'has_archive' => true,
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
        ),
        'taxonomies' => array(
            'post_tag',
            'category',
        )
    ));
    register_taxonomy_for_object_type('category','locations');
    register_taxonomy_for_object_type('post_tag','locations');
}
add_action('init','locations_post');


function locations_meta_box(){
    add_meta_box(
        'locations_fields_meta_box',//$id
        'Locations',//$title
        'show_locations_fields_meta_box',//$callback
        'locations',//$screen
        'normal',//$context
        'high'//$priority
    );
}
add_action('add_meta_boxes','locations_meta_box');


function show_locations_fields_meta_box(){
    global $post;
    $locations = get_post_meta($post->ID,'locations_fields',true);
    ?>
    <input type="hidden" name="locations_meta_box_nonce"value="<?php echo wp_create_nonce(basename(__FILE__)); ?> ">
    <p>
        <label for="locations_fields[text]">Phone</label><br>
        <input type="text" name="locations_fields[telephone]" id="locations_fields[telephone]" class="regular-text" value="<?php echo $locations['telephone'];?>">
    </p>
    <?php

}
//add_action('add_meta_boxes','show_locations_fields_meta_box');

function save_locations_fields_meta($post_id) {


    if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) {
        return$post_id;
    }
    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;
        }
    }

    $old = get_post_meta($post_id,'locations_fields',true);
    $new = $_POST['locations_fields'];
     update_post_meta($post_id,'locations_fields',$new);

}
add_action('save_post_locations','save_locations_fields_meta');

Upvotes: 1

Related Questions