Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
$5
WordPress Loops
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.
craigfarrall | 07/21/11 at 4:00am
Edit
Tutorial: How to assign prize money
(4) Possible Answers Submitted...
See a chronological view of answers?
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
-

Last edited:
07/21/11
4:21amSvilen Popov says:
<?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;
?>Previous versions of this answer: 07/21/11 at 4:21am
- 07/21/11 5:25am
craigfarrall says: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?
- 07/21/11 5:43am
Svilen Popov says:Replace query_posts('showposts=1&featured=yes&orderby=DESC'); with
query_posts('showposts=1&cat=22&tag=featured&orderby=DESC');
Where 22 is the category ID.
Also change the second query:
query_posts(array('post__not_in' => array($tmp_post_id), 'cat' => 22, 'showposts' => 5, 'orderby' => 'DESC'));
.
- 07/21/11 5:25am
-

Last edited:
07/21/11
5:46amRomel Apuya says: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..Previous versions of this answer: 07/21/11 at 5:44am | 07/21/11 at 5:46am
- 07/21/11 4:23am
Romel Apuya says:in regards to Svilen answer,
maybe you could try also
adding
<?php wp_reset_query(); ?>
before you next query_post.. - 07/22/11 5:35am
craigfarrall says: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? - 07/22/11 5:58am
Romel Apuya says: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; ?>
- 07/23/11 11:22am
Romel Apuya says:is this done?
please let us know..
- 07/21/11 4:23am
-

Last edited:
07/21/11
9:51amTeri Westerby says: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. -

Last edited:
07/23/11
11:48amjuaribd says:No need of custom field. The easiest way is to
1. Make the featured post as sticky. 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();
This question has expired.
Current status of this question: Community pot
Please log in to add additional discourse to this page.
Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.
If the asker does not get an answer then they have 10 days to request a refund.
