0

i having a problem on my theme.i created a custom meta box to select the post to be featured with a checkbox. in the loop its show the first checked posts,but i want to display just posted and checkbox checked in meta box post first.no matter what the last post with(checkbox checked) should display first.

i created metabox with this code:

function prfx_featured_meta() {
  global $current_user;
  if($current_user->roles[0] == 'administrator') {
  add_meta_box( 'prfx_meta', __( 'Featured Posts', 'sofol' ),     'prfx_meta_callback', 'post', 'normal', 'high' );
  }
}
add_action( 'add_meta_boxes', 'prfx_featured_meta' );
function prfx_meta_callback( $post ) {
wp_nonce_field( basename( __FILE__ ), 'prfx_nonce' );
$prfx_stored_meta = get_post_meta( $post->ID );
?>

 <p>
<span class="prfx-row-title"><?php _e( 'Check if this is a featured post: ', 'sofol' )?></span>
<div class="prfx-row-content">
    <label for="featured-checkbox">
        <input type="checkbox" name="featured-checkbox" id="featured-checkbox" value="yes" <?php if ( isset ( $prfx_stored_meta['featured-checkbox'] ) ) checked( $prfx_stored_meta['featured-checkbox'][0], 'yes' ); ?> />
        <?php _e( 'Featured Item', 'sofol' )?>
    </label>

</div>
</p>

<?php
}

function prfx_meta_save( $post_id ) {

// Checks save status - overcome autosave, etc.
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'prfx_nonce' ] ) && wp_verify_nonce( 
$_POST[ 'prfx_nonce' ], basename( __FILE__ ) ) ) ? 'true' : 'false';

// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
}

// Checks for input and saves - save checked as yes and unchecked at no
if( isset( $_POST[ 'featured-checkbox' ] ) ) {
update_post_meta( $post_id, 'featured-checkbox', 'yes' );
} else {
update_post_meta( $post_id, 'featured-checkbox', 'no' );
}
}
add_action( 'save_post', 'prfx_meta_save' );

heres my code for the loop:

<section class="featured-posts-area-wrap" id="featured">
<div class="container">
  <div class="stitle">
    Featured Posts
  </div>
  <?php  $args = array(
          'posts_per_page' => 3,
          'orderby' => 'featured-checkbox',
          'order' => 'DESC',
                'meta_query' => array(
                    array(
                    'key' => 'featured-checkbox',
                    'value' => 'yes'
            )
        )
      );
$featured = new WP_Query($args);
if ($featured->have_posts()): while($featured->have_posts()): $featured->the_post(); ?>
  <div class="featured-posts-contain">
    <article class="single-featured-post">
      <span class="viewsbar"><i class="fa fa-eye"></i> <?php echo sofol_get_post_views(get_the_ID()); ?></span>
      <?php
      // Feature post thumbnail.
      if ( has_post_thumbnail() ) {
        the_post_thumbnail();
      }
      else {
        echo '<img src="' . esc_url( get_template_directory_uri() ) . '/img/default.jpg" class="attachment-thumbnail wp-post-image" alt="Default image" />';
      } ?>
      <div class="post-grid-content-info">
        <span class="catbar"><?php the_category(); ?></span>
        <div class="meta"><span class="author">by <?php the_author_posts_link(); ?></span><span class="timeposted"><i class="fa fa-clock-o"></i> 11 hours ago</span> </div>
        <?php the_title( sprintf( '<h3><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h3>' ); ?>
      </div>
    </article>
  <?php endwhile; else: endif; wp_reset_postdata(); ?>
  </div>
</div>
</section>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.