1

the code for getting the metabox in image is:

function op_register_menu_meta_box() {
add_meta_box(
'op-menu-meta-box-id',
esc_html__( 'Custom offers Checklist (select any 2)', 'text-domain' ),
'op_render_menu_meta_box',
'listing'
);
}
add_action( 'add_meta_boxes_listing', 'op_register_menu_meta_box' );
function op_render_menu_meta_box() {
// Metabox content
$getPostsToSelect = get_posts('post_type=offers&numberposts=-1');
foreach ($getPostsToSelect as $aPostsToSelect) {
?>
<label>
<input 
  type='checkbox' 
  name='yourcustom_meta[]' 
  class='postsToSelect'
  value='<?php echo $aPostsToSelect->ID ?>'
 /> 
<?php echo $aPostsToSelect->post_title;
?>
</label><br />
<?php
}}

Currently i have got to this on edit post page!

enter image description here

the code of archive-offers.php is: and i want to print it on archive page

<?php
/**
* The main template file***/
get_header(); ?>
<?php
        /**
        * The main template file
        * It puts together the home page if no home.php file exists.
        *
        * @package Simple Blog Theme
        */
        get_header(); ?>
            <div class="row mb-5">
                <!-- Blog Entries Column -->
                <div class="col"></div>
                <div class="col-sm-9">
                    <article itemscope itemtype="http://schema.org/Article">
                        <span itemscope itemprop="mainEntityOfPage" itemType="https://schema.org/WebPage" itemid="https://google.com/article" /></span>
                        <div class="my-5">
                        </div>
                        <!-- Blog Post -->
**I want to print 1st offer here!** **I want to print 2nd offer here!**
                        <?php
                    // Note: this loop is a simplified version of a loop published in a post at Elegant Themes
                    // Source: https://www.elegantthemes.com/blog/tips-tricks/converting-html-sites-to-wordpress-sites
                    if ( have_posts() ) : while ( have_posts() ) : the_post();  ?>
                            <div>
                                <h1 class="card-title mb-4" itemprop="headline">
                                    <span itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
        <meta itemprop="name" content="Rookie Bloggers">
        <meta itemprop="url" content="http://sample1.nepromedia.com/wp-content/uploads/2018/02/logo.jpg">
        <span itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
        <meta itemprop="url" content="http://sample1.nepromedia.com/wp-content/uploads/2018/02/logo.jpg"></span>


                                    <?php the_title(); ?>
                                </h1>
                                <div class=" ml-1 mr-1 row alert alert-info text-info mb-5">
                                    <span class="col-sm"><i class="fas fa-edit "></i> Last Modified On</span> <span class="col-sm" itemprop="datePublished">
                                <meta itemprop="dateModified" content="<?php the_modified_date(); ?>"><strong><i class="fas fa-calendar-alt "></i>
                        <?php the_time( 'F j, Y' ); ?></strong></span>
                                    <span class="col-sm" itemprop="author" itemscope itemtype="http://schema.org/Person">
                            <meta itemprop="name" content="<?php the_author(); ?>">
        <meta itemprop="publisher"><?php $args = array('class' => 'nimesh12' , ); ?> <?php echo get_avatar( get_the_author_meta( 'ID' ), 25, $default, $alt, $args); ?>
        <?php the_author_posts_link(); ?></span>
                                </div>

                                <span itemprop='image' itemscope itemtype='http://schema.org/ImageObject'><meta itemprop="url"content="<?php echo get_the_post_thumbnail_url($post_id); ?>"><?php the_post_thumbnail( 'single-post', array('class' => 'img-fluid') ); ?></span>
                                <div>
                                    <p class="card-text mb-5" itemprop="description">
                                        <?php the_content(); ?>
                                    </p>
                                    <?php
                        $prev_post = get_previous_post();
                        if (!empty( $prev_post )): ?>
                                        <strong><a href="<?php echo $prev_post->guid ?>"><?php echo 'Older posts' ?></a></strong>
                                        <?php endif ?>
                                </div>

                            </div>
                            <?php endwhile; /* rewind or continue if all posts have been fetched */ ?>

                            <?php else : ?>
                            <?php endif; ?>
                    </article>
                </div>
                <div class="col"></div>
            </div>
            <!-- /.row -->
            </div>
            <!-- /.container -->
            <?php get_footer(); ?>

Mechanism: when i check two of them and publish them, they should remain checked here! After that those selected Custom posts (offer) should be printed on archive-offer.php

what should i do to achieve this? Help Appreciated!

  • Share the code that outputs the "Custom Offer Section" meta box. Secondly, elaborate "printed on archive-offer.php"; i.e., share the code of the archive-offer.php file, and mark where should the selected "offers" should be added to. (And a sample markup/HTML of the "offer" like <h3><?php the_title(); ?></h3><p><?php the_content(); ?></p>) – Sally CJ Mar 23 '18 at 6:15
  • Look lookup. add_meta_box() and get_post_meta() – TurtleTread Mar 23 '18 at 6:26
  • Thank you! here is my link to the question, its explained in detail. @SallyCJ i have added get header and footer in archive-offers.php wordpress.stackexchange.com/questions/298610/… – Nimesh Mar 23 '18 at 6:38
  • 1
    See my answer here. – Sally CJ Mar 28 '18 at 7:09
  • 1
    It is solved i followed your link! @SallyCJ – Nimesh Mar 28 '18 at 9:36
2

The feature is achieved in this way!

function op_register_menu_meta_box() {
add_meta_box(
    'op-menu-meta-box-id', // Metabox ID
     esc_html__( 'Custom offers Checklist (select any 2)', 'text-domain' ), //Metabox Title
    'op_render_menu_meta_box', // Metabox Callback Function
    'listing' // Place Where this Metbax Should Appear
    );
}
add_action( 'add_meta_boxes_listing', 'op_register_menu_meta_box' ); //Metabox Hook

The callback function!

function op_render_menu_meta_box( $post ) { // Metabox Callback Function Called
// Get the selected 'offers' (array of post IDs).
$offers_ids = (array) get_post_meta( $post->ID, '_offers_ids', true );

// Displays a nonce field, without referrer field.
wp_nonce_field( 'save-offers-meta', 'offers_meta_nonce', false );

// Metabox content
$getPostsToSelect = get_posts(
                    array(
                        'post_type' => 'offers',
                        'posts_per_page' => 3,
                    ));
foreach ($getPostsToSelect as $aPostsToSelect) {
?>
<label>
    <input 
      type='checkbox' 
      name='offers_meta[]' 
      class='postsToSelect'
      value='<?php echo $aPostsToSelect->ID ?>'
      <?php checked( true, in_array( $aPostsToSelect->ID, $offers_ids ) ); ?>
     /> 
    <?php echo $aPostsToSelect->post_title;
?>
</label><br/>
<?php
}}

Save Metabox function!

add_action( 'save_post', 'op_save_offers_meta', 10, 2 );
function op_save_offers_meta( $post_id, $post ) {
    // Make sure that we can save the meta data.
    if ( ( empty( $post_id ) || empty( $post ) || ( 'listing' !== get_post_type( $post ) ) )
    || ( empty( $_POST['post_ID'] ) || $_POST['post_ID'] != $post_id || ! isset( $_POST['offers_meta'] ) )
    || ( empty( $_POST['offers_meta_nonce'] ) || ! wp_verify_nonce( $_POST['offers_meta_nonce'], 'save-offers-meta' ) )
    || ( ! current_user_can( 'edit_post', $post_id ) ) ) {
        return;
    }

    $offers_ids = wp_parse_id_list( $_POST['offers_meta'] );
    if ( ! empty( $offers_ids ) ) {
        update_post_meta( $post_id, '_offers_ids', $offers_ids );
    } else {
        delete_post_meta( $post_id, '_offers_ids' );
    }
}

At last the procedure to echo checked !

<?php $post_id = get_the_ID();

// Get the selected 'offers' (array of post IDs).
$offers_ids = (array) get_post_meta( $post_id, '_offers_ids', true );

$q = new WP_Query( [
    'post_type' => 'offers',
    'post__in'  => $offers_ids,
] );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
    ?>
        <h3><?php the_title(); ?></h3>
        <p><?php the_content(); ?></p>
    <?php
    }
}

wp_reset_query ?>

Big thanks to @sallycj

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.