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

Need to Display 5 Posts (excluding ones that i'm hiding) WordPress

  • SOLVED

Ok i asked a question about archiving expired posts (based on custom field) yesterday: (http://wpquestions.com/question/show/id/583) and i have it working correctly.

Now my issue is that in my loop, i have it set to display 5 posts. Problem right now that if there are any expired posts within that set of the first 5 posts then it will not display.

Example ...My Last 5 Posts

1. Not expired: is showing up.
2. Expired: not showing up
3. Expired: not showing up
4. Not expired: is showing up.
5. Not expired: is showing up.

Therefore on my front page, i am displaying only 3 posts, when i want to show 5.

Here's what my initial code for my loop looks like on home page:


<?php if(have_posts()) : ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=-190&posts_per_page=5&paged=$paged"); ?>

<?php while(have_posts()) : the_post(); ?>

<?php //to check against expiration date;

$currentdate = strtotime(date("m/d/Y"));
$expirationdate = get_post_custom_values('deadline');
if (is_null($expirationdate)) {
$expirestring = '50503000'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {

if (is_array($expirationdate)) {
$expirestringarray = implode($expirationdate);
}
$expirestring = strtotime($expirestringarray);
}
if (( $expirestring > $currentdate ) || (is_archive())) { ?>

Answers (2)

2010-07-07

Oleg Butuzov answers:

<strong>exclude</strong> key for query posts. comma separated ids of exclude posts.


Oleg Butuzov comments:

other example
http://www.smashingmagazine.com/2009/06/10/10-useful-wordpress-loop-hacks/
second topic.
second example


Oleg Butuzov comments:

http://codex.wordpress.org/Template_Tags/query_posts
'post__not_in' => array(6,2,8)


cdogstu99 comments:

This isn't going to work for me because on my site i list deals with an expiration date (or custom field). The point of the above code is for these deals with an expired date to disappear. I can't manually go in and specify each post to exclude since there are so many and this would take me forever.


Oleg Butuzov comments:

well its can work, but you will need to select a all post ids with a expired posts.

wpdb->get_col() will give you a required array to use post__not_in


cdogstu99 comments:

hmm..i'm not really much of a programmer..so don't quite follow..can you show me some code.


Oleg Butuzov comments:

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
query_posts("cat=-190&posts_per_page=5&paged=$paged"); ?>

<?php
query_posts(
'cat' => -190,
'posts_per_page'=>5,
'paged' => $paged,
'post__not_in' => $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'expier' AND meta_value < NOW() ");
);
?>


but this code needs tweaks. 1) i ahve no ide whats value youare storing there. if int thats good! so in where conditions you need to set the proper meta_key and correct condition to select only exired dates.


cdogstu99 comments:

so i tried this:

<?php

query_posts(

'cat' => -190,

'posts_per_page'=>5,

'paged' => $paged,

'post__not_in' => $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'deadline' AND meta_value < NOW() ");



?>

<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>

and got this error:

syntax error, unexpected T_DOUBLE_ARROW on line 13

am i right with my code?


Oleg Butuzov comments:

<?
query_posts(
'cat' => -190,
'posts_per_page'=>5,
'paged' => $paged,
'post__not_in' => $wpdb->get_col("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'deadline' AND meta_value < NOW() ")
);

?>


you need to change meta_value < NOW() condition for your needs. NOW() is datetime object. meta_value is a string. you can somehow convert it to date time or convert it to int and use ".time()." instead NOW()


Oleg Butuzov comments:

http://dev.mysql.com/doc/refman/5.1/en/charset-convert.html

2010-07-08

rilwis answers:

I think you just change the condition to make all posts are shown in homepage, like this:

if (($expirestring > $currentdate) || is_archive() || is_home()) { ?>


cdogstu99 comments:

this doesn't work..expired posts are still showing up