2

I am trying to display an post to my home page based on a selection. What I mean is in the backend I have a custom post that has a metabox labeled Assign to Home with a drop down that has a selection of Yes or No. What I am trying to do is when a custom post has a selection of Yes it will be displayed on the home page. When a selection says No it will not be displayed.

The code below is what I am trying to use to call my post. the Post Type and meta key are both correct but when I try and display my post I get an array.

home page

 <?php function posts_draft($meta_values) {
    $args_draft = array(
      'numberposts'   => 3,
      'post_type'     => 'fe', // set you custom post type
      'meta_key'      => '_cmb_homeDisplay',
      'meta_value'    => $meta_values,
    );
     $my_posts_draft = get_posts( $args_draft ); 
     $posts_draft = get_posts($my_posts_draft);
       return $posts_draft;
     }
     {
      echo posts_draft('yes');}
 ?>

  <?php if ( $fleet->have_posts() ) : while ( $fleet->have_posts() ) : $fleet->the_post(); ?>     
    <div class="fleetBox">
    <img src="<?php echo get_post_meta($post->ID, "_cmb_limoThumbnail", true); ?>" />
       <ul>
            <li><h3><?php the_title( '' ); ?></h3></li>
            <li><h4>Luxury <?php echo get_post_meta($post->ID, '_cmb_limo_select', true); ?></h4></li>
            <li><a href="<?php echo get_permalink(); ?>">More Details</a></li>
        </ul>
    </div>
 <?php endwhile; endif; ?>

I know this may not be that helpful because its a mall amount of code but this is what I am using to display my drop down. This code is a small snippet in the full custom post type that I am using.

The post Type being used for this snippet is fe and the meta key that gets assigned to all of my prefix id is cmb

  array(
    'name'    => 'Display Home',
    'desc'    => 'Select an option',
    'id'      => $prefix . 'homeDisplay',
    'type'    => 'select',
    'options' => array(
       'custom' => __( 'Select...', 'cmb' ),
       'standard'   => __( 'Yes', 'cmb' ),
       'none'     => __( 'No', 'cmb' ),
 ),
       'default' => 'custom',
 ),
  • 1
    Can I ask why you're using get_posts twice? Also, the loop beneath that code in your first snippet isn't the loop to display those posts checked from the meta box right? That's some other main loop you have and your meta box checked posts should appear above that? – RachieVee Dec 17 '14 at 21:50
2

I looks like you are using the custom meta boxes and fields github repo, which is an awesome library. I think the primary thing you need to do is a new WP_Query. Below is an example of that in action. The only difference is that in my example I made the "Assign to Homepage" a checkbox on the custom post type.

<?php
   $args = array(
   'post_type'  => 'testimonial',
   'meta_key'   => '_wla_homepage_slider_checkbox',
   'meta_value' => 'on',
   'post_per_page' => 100, /* add a reasonable max # rows */
   'no_found_rows' => true, /* don't generate a count as part of query, unless you need it. */
   );
 $testimonials = new WP_Query( $args );
?>

From there you can run through your custom loop and pull meta data as needed.

  • Perfect thank you so much!!! You are correct about repo its an amazing library. I make use the checkbox idea instead of the drop down. Thank you again @mrbobbybryant – user3756781 Dec 18 '14 at 14:03
  • would you know why the post_per_page goes past the limit? @mrbobbybryant – user3756781 Dec 18 '14 at 14:09
  • As in you are seeing more than 100 posts_per_page? In this example 100 is an arbitrary number. What is important is that you limit the query for performance reasons. – mrbobbybryant Dec 18 '14 at 14:23
  • I just got it to work. I added showpost=3, and I was able to set my limit. Thank you again for your help @mrbobbybryant – user3756781 Dec 18 '14 at 14:25
-1
<?php
   $args = array(
   'post_type'  => 'product', /* your Custom post slag */
   'meta_key'   => '_wla_homepage_slider_checkbox',
   'meta_value' => 'on',
   'posts_per_page' => 15, 
   'no_found_rows' => true
   );
 $query= new WP_Query( $args );
?>
  • 1
    Please don't just post code; please explain what it does and how it answers the question. – Tim Malone Jul 21 '16 at 7:42

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.