• Basically I’m taking posts and sorting them by custome field ‘date’ and then displaying them, I am doing so in a widget using the first bit of code and it works great, the second bit of code is meant for archive.php and it works in sorting the posts but I don’t want to display the posts once it has been passed todaysdate

    First bit of code

    <?php // Get today's date in the right format
    $todaysDate = date('d/m/y H:i:s');
    ?>
    ?
    <?php query_posts('showposts=5&category_name=Events - NSW&meta_key=Date&meta_compare=>=&meta_value=' . $todaysDate . '&orderby=meta_value&order=ASC'); ?>
    <ul>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <li>
    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    	<p><?php the_excerpt(); ?></p>
    </li>
    <?php endwhile; else: ?>
    <li>Sorry, no upcoming events!</li>
    <?php endif; ?>
    </ul>
    <?php wp_reset_query(); ?>

    I’m trying to do the same in archive.php to sort the posts in the category page in the same manner, so I am using this and it works

    <?php
    if( is_category('5') ) {
    	//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args= array(
    		'meta_value' => $todaysDate,
    		'meta_key' => 'date',
    		'orderby' => 'date',
    		'order' => 'ASC',
    		'cat' => 5,
    		'paged' => $paged
    	);
    	query_posts($args);}
    ?>

    As I said near the top of the post, I want to add something to this code in archive.php to exclude the posts that have passed todaysdate

Viewing 7 replies - 1 through 7 (of 7 total)
  • You may be able to use meta_compare as documented in the Codex query_posts:Custom Field Parameters

    Thread Starter moebin

    (@moebin)

    I couldn’t find what I was looking for there…

    I need the second bit of code to exclude a post from the category page once that date is less than $todaysdate

    Its in the meta_compare description:

    $args= array(
             'meta_value' => $todaysDate,
             'meta_compare' => '>=',
             'meta_key' => 'date',
             'orderby' => 'date',
             'order' => 'ASC',
             'cat' => 5,
             'paged' => $paged
          );
          query_posts($args);
    Thread Starter moebin

    (@moebin)

    That hasn’t done it…

    I think it needs an if else scenario

    if meta key of a post is <$todaysdate then don’t show it

    That’s what I need but I can’t get it to work in code.

    Thread Starter moebin

    (@moebin)

    Ok I’ll try to make things clearer,

    The first bit of code works and I hope people around here can make some use out of it, we’re using it in some of the widgitized space to display posts from a specific category that are events, they are sorted by event date and events that have passed i.e. posts with event dates that less than todays date are not displayed, we used the php code plugin to put the php code in the widget.

    The second bit of code is intended to do a very similar job in the event category page, so when visitors click on events in the navbar above, it displays the category page with the events (posts) sorted by event date (meta_key)… this after some modicifications is now working, we have it sitting in the archive.php.

    The further feature that we need from this code is to not display the events (posts) that have passed. i.e. events with the meta_key date < $todaysdate

    hopefully that makes things clearer sometimes when you’ve been working on a problem for a while it’s all in your head and you neglect to explain it properly.

    could you post the whole archive.php into a pastebin https://wordpress.pastebin.com/ to see if there is a possible other cause outside the snippet that you’ve already posted?

    also, to debug things, you could temporarily echo some essential variables in your code, to be sure the numbers are as you expect them to be.

    The further feature that we need from this code is to not display the events (posts) that have passed. i.e. events with the meta_key date < $todaysdate

    This is EXACTLY what the meta_compare example is supposed to do. My guess is that the format of the date in your custom field is not the same as that needed to compare to $todaysDate. Print out $todaysDate and see if it matches the format of the custom field. Both need to be in the format ‘YYYY/MM/DD’ or similar, with the year first, then month, then day, to compare correctly.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘excluding posts via custom field’ is closed to new replies.