lucbord
5/22/2017 - 10:50 AM

CHECBOX FOR DIOCESI POST

CHECBOX FOR DIOCESI POST

add_action('post_submitbox_misc_actions', createCustomField);
add_action('save_post', saveCustomField);

function createCustomField()
{
    $post_id = get_the_ID();
  
    if (get_post_type($post_id) != 'post') {
        return;
    }
  
    $value = get_post_meta($post_id, '_my_custom_field', true);
    wp_nonce_field('my_custom_nonce_'.$post_id, 'my_custom_nonce');
    ?>
    <div class="misc-pub-section misc-pub-section-last">
        <label><input type="checkbox" value="1" <?php checked($value, true, true); ?> name="_my_custom_field" /><?php _e('My Custom Checkbox Field', 'pmg'); ?></label>
    </div>
    <?php
}

function saveCustomField($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    
    if (
        !isset($_POST['my_custom_nonce']) ||
        !wp_verify_nonce($_POST['my_custom_nonce'], 'my_custom_nonce_'.$post_id)
    ) {
        return;
    }
    
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    if (isset($_POST['_my_custom_field'])) {
        update_post_meta($post_id, '_my_custom_field', $_POST['_my_custom_field']);
    } else {
        delete_post_meta($post_id, '_my_custom_field');
    }
}

PAGE TEMPLATE

<?php $args = array(
        'meta_key' => '_my_custom_field', // include underscore prefix in key name
        'meta_value' => 1
    );
    // The number of posts displayed would be determined under Settings->Reading
    query_posts($args);

    if(have_posts()): while(have_posts()): the_post(); ?>
       
        <h1><?php the_title(); ?></h1>
    
    <?php endwhile; else: ?>

    <?php endif;    ?>