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="https://wordpress.stackexchange.com/questions/172557/<?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',
 ),

2 Answers
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.

Leave a Reply

Your email address will not be published. Required fields are marked *