0

I have created a function for saving posts as "featured" and showing them on the homepage. It works with a custom checkbox in the admin panel. I did the function with help on forums but I would like to add to this function the ability to set a specific order number to every featured post and show them at the front page in this order. (choosing which one is the first, which is the second, third, etc…)

// FEATURED POST FUNCTION

add_action('add_meta_boxes', 'add_checkbox_featured');

function add_checkbox_featured() {
add_meta_box('is_featured', 'Featured', 'print_checkbox_featured', 'post', 'side');}

function print_checkbox_featured() {
global $post;
$checked = get_post_meta($post->ID, '_featured', true) ? 'checked="checked"' : '';
echo '<label for="checkbox_is_featured">Show at the front page <input id="checkbox_is_featured" name="is_featured" type="checkbox" value="1" '.$checked.'/</label>';
}

add_action('save_post', 'save_checkbox_featured');

function save_checkbox_featured($post_id){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

   if ( !current_user_can( 'edit_post', $post_id ) )
            return $post_id;

   if ($_POST['is_featured']){
            add_post_meta($post_id, '_featured', '1');
    }else{
            delete_post_meta($post_id, '_featured');
    }
 }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

0

Try this for adding order for the featured post but on home page you have to use meta_query to display posts by entered order

add_action('add_meta_boxes', 'add_checkbox_featured');
//add_action('add_meta_boxes', 'add_checkbox_featured_order');

function add_checkbox_featured() {
add_meta_box('is_featured', 'Featured', 'print_checkbox_featured', 'post', 'side');
}

/*function add_checkbox_featured_order() {
add_meta_box('is_featured_order', 'Featured Order', 'print_checkbox_featured_order', 'post', 'side');
}

function print_checkbox_featured_order() {
global $post;

}*/


function print_checkbox_featured() {
global $post;
$checked = get_post_meta($post->ID, '_featured', true) ? 'checked="checked"' : '';
echo '<label for="checkbox_is_featured">Show at the front page <input id="checkbox_is_featured" name="is_featured" type="checkbox" value="1" '.$checked.'/></label>';

$forder = get_post_meta($post->ID, '_featured_order', true);
echo '<label for="checkbox_is_featured_order">Enter featured order
<input id="checkbox_is_featured_order" name="is_featured_order" type="text" value="'.$forder.'"/></label>';
}

add_action('save_post', 'save_checkbox_featured');

function save_checkbox_featured($post_id){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

   if ( !current_user_can( 'edit_post', $post_id ) )
            return $post_id;

   if ($_POST['is_featured']){
            add_post_meta($post_id, '_featured', '1');
    }else{
            delete_post_meta($post_id, '_featured');
    }

   if ($_POST['is_featured_order']){
            add_post_meta($post_id, '_featured_order', $_POST['is_featured_order']);
    }else{
            delete_post_meta($post_id, '_featured_order');
    }
 }
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • Hi, thanks for your answer! i had this code for the home page `query_posts('meta_key=_featured'); ` and now i tried with something like below but is not showing the post in the correct order. `query_posts(array('meta_query'=>'_featured_order', 'meta_key'=>'_featured')); ` also I tried using only but is not working `query_posts('meta_query=_featured_order');` – user2807090 Sep 23 '13 at 18:40
  • and last question, How I can show in the admin panel only one Meta box with the 2 checkboxes, Featured and Featured Order? Thank you! – user2807090 Sep 23 '13 at 18:58
  • See my updated answer for showing both fields in same meta box , for order by google it http://wordpress.org/support/topic/meta_query-order-by-multiple-keys – M Khalid Junaid Sep 23 '13 at 19:33
  • Ok, the last question was something I thought and i tried but as i can't see the posts listed in the correct order I didn't know if it was working ok. The orderby problem is something very confusing for me, i tried many things but… i think it's too hard for a non programmer like me. thanks anyway – user2807090 Sep 23 '13 at 21:12
  • @user2807090 Please refer to this question http://stackoverflow.com/questions/12717408/wp-query-filter-by-multiple-meta-values-and-order-by-other-meta-values i guess this what you are looking for and my answer's concerned is to show how to add 2nd field in meta box not more than that so for order by can provide the links only – M Khalid Junaid Sep 24 '13 at 06:00