Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WordPress Loops WordPress

Hi,

I am developing a new website for a client using WordPress and this is what I am looking at achieving:

I want to display 5 posts within a certain category but within those five posts I want the first post within that category to be a featured post, this post will be marked with a custom field of "featured".

Obviously if the featured post has been set I don't want it to appear again on the list of posts.

So an overview: I want 5 posts, the first one to be a featured post and the other 4 posts to go in order of date.

Anyone got any ideas?

Thanks.

Answers (4)

2011-07-21

Svilen Popov answers:


<?php
query_posts('showposts=1&featured=yes&orderby=DESC');
if (have_posts()) : while (have_posts()) : the_post();
$tmp_post_id = get_the_ID();
?>
// display the post
<?php endwhile; endif; ?>

<?php
query_posts(array('post__not_in' => array($tmp_post_id), 'showposts' => 5, 'orderby' => 'DESC'));
if (have_posts()) : while (have_posts()) : the_post();
?>
// display the posts
<?php
endwhile;
endif;
?>


craigfarrall comments:

Thanks for you reply.

Those posts aren't categorised, could you advise how best to do this?

Also, featured=yes doesn't seem to call the custom field called "featured", how can I call on the custom field?


Svilen Popov comments:

Replace <em>query_posts('showposts=1&featured=yes&orderby=DESC');</em> with

<blockquote>query_posts('showposts=1&<strong>cat=22&tag=featured</strong>&orderby=DESC');</blockquote>

Where <strong>22</strong> is the <strong>category ID</strong>.

Also change the second query:

<blockquote>query_posts(array('post__not_in' => array($tmp_post_id), <strong>'cat' => 22,</strong> 'showposts' => 5, 'orderby' => 'DESC'));</blockquote>.

2011-07-21

Romel Apuya answers:

how about trying this:


<?php

query_posts($query_string.'&meta_key=YOURFIELDNAME&meta_value=YOURVALUEHERE');

if (have_posts()) : while (have_posts()) : the_post();

?>

//display feature post.. which has custom field 'featured';

<?php endwhile; endif; ?>

<?php wp_reset_query(); ?>

<?php

query_posts($query_string.'&meta_key!=YOURFIELDNAME&meta_value!=YOURVALUEHERE&order=desc'));

if (have_posts()) : while (have_posts()) : the_post();

?>

// display the posts

<?php

endwhile;

endif;

?>




where YOURFIELDNAME is the name of your custom field and YOURFIELDVALUE is the value..

hope this works..


Romel Apuya comments:

in regards to Svilen answer,
maybe you could try also

adding
<?php wp_reset_query(); ?>

before you next query_post..


craigfarrall comments:

Using the both codes provided I have amended and used bits of both and here is the final code:

<?php query_posts($query_string.'&meta_key=featured&meta_value=1&cat=4');
if (have_posts()) : while (have_posts()) : the_post();
$tmp_post_id = get_the_ID(); ?>

<li class="featured"><a href="<?php if ( get_post_meta($post->ID, 'url', true) ) { ?><?php echo get_post_meta($post->ID, "url", $single = true); ?><?php } ?>" title="<?php the_title(); ?>" target="_blank"><?php echo ShortenText(get_the_title()); ?></a></li>

<?php endwhile; endif; ?>

<?php query_posts(array('post__not_in' => array($tmp_post_id), 'cat' => 4, $query_string.'&meta_key!=featured', 'showposts' => 4, 'orderby' => 'DESC'));
if (have_posts()) : while (have_posts()) : the_post(); ?>

<li><a href="<?php if ( get_post_meta($post->ID, 'url', true) ) { ?><?php echo get_post_meta($post->ID, "url", $single = true); ?><?php } ?>" title="<?php the_title(); ?>" target="_blank"><?php echo ShortenText(get_the_title()); ?></a></li>

<?php endwhile; endif; ?>

Another question though, I am looking at having 5 posts to show altogether so one featured and then 4 other posts. But incase the client selects more than one featured posts I owuld like the 4 other posts to then be limited to 3 posts, so the posts will always be five posts maximum, but depending on how many featured posts have been set depends on how many other posts are displayed. Any way of doing this?


Romel Apuya comments:

you just have to count the number of featured post.
then subtract it from 5 and use the value on the second loop
to the showposts.

somehting like.



$limitcounter=0;

<?php query_posts($query_string.'&meta_key=featured&meta_value=1&cat=4');
if (have_posts()) : while (have_posts()) : the_post();
$tmp_post_id = get_the_ID(); ?>
$limitcounter++;
<li class="featured"><a href="<?php if ( get_post_meta($post->ID, 'url', true) ) { ?><?php echo get_post_meta($post->ID, "url", $single = true); ?><?php } ?>" title="<?php the_title(); ?>" target="_blank"><?php echo ShortenText(get_the_title()); ?></a></li>

<?php endwhile; endif; ?>

$yourlimit = 5- $limitcounter;

<?php query_posts(array('post__not_in' => array($tmp_post_id), 'cat' => 4, $query_string.'&meta_key!=featured', 'showposts' => $yourlimit, 'orderby' => 'DESC'));
if (have_posts()) : while (have_posts()) : the_post(); ?>

<li><a href="<?php if ( get_post_meta($post->ID, 'url', true) ) { ?><?php echo get_post_meta($post->ID, "url", $single = true); ?><?php } ?>" title="<?php the_title(); ?>" target="_blank"><?php echo ShortenText(get_the_title()); ?></a></li>

<?php endwhile; endif; ?>


Romel Apuya comments:

is this done?
please let us know..

2011-07-21

Teri Westerby answers:

Quite simply: sticky post

It's a plugin that allows you to pick any post and "stick" it to the first post placement until you decide to change it. The rest descend accordingly. Like a forum.

2011-07-23

juaribd answers:

No need of custom field. The easiest way is to
1. Make the featured post as <strong>sticky</strong>. Then use the following code:



$sticky = get_option('sticky_posts');
query_posts(array(
'post__in' => $sticky,
));

<ul>
<?php $recent = new WP_Query("howposts=1"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a>
<?php the_excerpt(); ?> </li>
<?php endwhile; ?>
</ul>

query_posts(array(
'post__not_in' => $sticky;
));

<ul>
<?php $recent = new WP_Query("howposts=4"); while($recent->have_posts()) : $recent->the_post();?>
<li><a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_title(); ?>
</a></li>
<?php endwhile; ?>
</ul>

wp_reset_query();