0

What i am trying to achieve is this

I have created a custom post(offer), i have another custom post created (listing).

I want to add a metabox in listing, which will recieve check list of all offers

enter image description here

i have achieved this as shown in image using code below.

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
}}

Now what what i want to get is out of the several checklist of offers, we can select two offers by checking them.

Q1. How to save the checked boxes?

Q2. How to use them *(the checked only) later to display on single.php?

And then when we publish the listing, the checked offers will be printed on archive-offers.php

I need to achieve this, please help me!

  • jQuery is javascript, functions.php is PHP, there's further research you need to do, e.g. what are metaboxes, how do you add a metabox, how do you load javascript in WP, how do you load it in the admin area, and how to do you use javascript with a metabox – Tom J Nowell Mar 22 '18 at 16:46
  • i have updated my requirement! @TomJNowell Pleae help me achieve the feature – Nimesh Mar 22 '18 at 18:29
  • hmmm now that you've updated your question, I don't see what jQuery or javascript have to do with it, it can all be done with PHP. I take it the problem you're grappling with is that you can display the checkboxes but you don't know how to save the value? – Tom J Nowell Mar 22 '18 at 19:38
  • 1
    I updated my question @TomJNowell please guide me with code to achieve it. – Nimesh Mar 23 '18 at 1:09
  • 1
    You can use the WordPress checked function for rendering as checked: codex.wordpress.org/Function_Reference/checked – Pabamato Mar 27 '18 at 2:41
3

You can use PHP in your functions.php file to insert inline JavaScript (including jQuery) using WordPress hooks.

In your example, you could use the admin_print_scripts-post.php action hook to print your inline jQuery like so:

function wpse_admin_print_scripts_edit() {
  echo "<script>var limit = 2;' .
    'jQuery('input.single-checkbox').on('change', function(evt) {' .
      'if(jQuery('input.single-checkbox:checked').length > limit) {' .
        'this.checked = false;' .
      '}' .
    '});</script>";
}
add_action( 'admin_print_scripts-post.php', 'wpse_admin_print_scripts_edit' );
| improve this answer | |
  • I have updated my question, Please help me achieve the feature. Thank you! – Nimesh Mar 22 '18 at 18:25

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.