I have a custom wordpress function that shows the number of featured recipes. The function works fine when showposts=-1
is in the query. However when I put ‘showposts=5’ only two of my posts are shown.
Below is my function
function pika_featured_recipes_shortcode() {
ob_start();
echo '<div class="featured-box-heading"> Featured Recipes </div>';
echo '<div class="featured-box">';
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5&post_type=cp_recipe&order=Desc&orderby=date' . '&paged=' . $paged);
while ($wp_query->have_posts()):
$wp_query->the_post();
$entry_id = get_the_ID();
$entry_link = get_permalink($entry_id);
$entry_image = get_post_thumbnail_id($entry_id);
$entry_title = get_the_title($entry_id);
$entry_description = get_post_meta($entry_id, '_cp_recipe_short_description', true);
$entry_excerpt = get_post_meta($entry_id, '_cp_recipe_excerpt', true);
$likes = get_post_meta( $entry_id, '_cp_likes', true );
if (get_field('featured-pika-recipe') == 'Yes') {
echo '<div class="featured-result-box item ">
<div class="box">
<div class="pika-featured-box-img">';
if(!empty($entry_image)) {
echo '<a href="'.$entry_link.'">'.wp_get_attachment_image($entry_image, 'cp_298_192').'</a>';
}
echo' </div><!-- /.box-img -->';
echo'<div class="box-entry">
<h5 class="pika-featured-title"><a href="';
echo $entry_link;
echo '">';
echo $entry_title;
echo'</a></h5>';
echo $trimmed = wp_trim_words( $entry_description, $num_words = 15, $more = null );
echo'</div><!-- /.box-entry -->';
echo'</div>';
echo'</div>';
echo'<div style="clear:both"></div>';
}
endwhile;
wp_reset_query();
echo '</div>';
$output = ob_get_clean();
return $output;}
add_shortcode( 'pika_featured-recipes', 'pika_featured_recipes_shortcode' );
The problem seems to be with
if (get_field('featured-pika-recipe') == 'Yes') {
removing this and the number of posts appear fine. Any way i can resolve this?
maybe because only 2 posts ( of the last 5 posts that you actually call ) got
get_field('featured-pika-recipe') == 'Yes'
you can try to add this custom field directly to your query to get last 5 posts with this custom field with following code