WordPress.org

Ready to get started?Download WordPress

Forums

Order custom posts by custom field value, exclude by date (12 posts)

  1. Ray Gulick
    Member
    Posted 3 years ago #

    I've done some research and found separate solutions for ordering custom posts by custom field value and excluding posts by date. But I cannot figure out how to do both: have tried various ways of stringing different solutions together.

    I'm trying to show events that are ordered by a custom field (order-date) that is in Y-m-d format. That part works until I add the code meant to filter out past dates ('meta_value' >= $today). Once that's added, it displays unfiltered blog posts, rather than "performance" custom posts.

    Here is the code I'm currently using:

    <?php
    $today = date('Y-m-d');
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts((array('post_type' => 'performance',
    'posts_per_page' => 5,
    'caller_get_posts' => 5,
    'paged' => $paged,
    'meta_key' => 'order-date',
    'orderby' => 'meta_value',
    'order' => 'ASC'
    )) && ('meta_value' >= $today));
     if (have_posts()) :
    while (have_posts()) : the_post();
    ?>

    Obviously, the code meant to filter out past events is not working. How can I filter past events using the "order-date" custom field?

  2. Peter vanDoorn
    Member
    Posted 3 years ago #

    A quick look suggests that it might be that your 'meta_value' >= $today is actually saying "greater than or equal to today". Try < $today (less than) or != $today (not equal to)

  3. Ray Gulick
    Member
    Posted 3 years ago #

    Thanks, Peter. Tried your suggestion. Doesn't matter what I use, it breaks it and shows blog posts rather than custom posts (and greater than or equal to today is what I want: displaying posts for future events).

    Here is the code I'm currently using to order the custom posts. Have tried various things to filter out past events, but not found anything that doesn't break things in one way or another:

    <?php
    	$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    	query_posts(array(
     		'post_type' => 'performance',
    		'posts_per_page' => 5,
    		'caller_get_posts' => 5,
    		'paged' => $paged,
    		'meta_key' => 'order-date',
    		'orderby' => 'meta_value',
    		'order' => 'ASC'
    		));
    	if (have_posts()) :
    	while (have_posts()) : the_post();
    ?>
  4. Ray Gulick
    Member
    Posted 3 years ago #

    Here's the code I got on another forum. Sorts the way I need it to, but still does not filter out past performance events. Anyone see the problem?

    <?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    query_posts(array(
    'post_type' => 'performance',
    'posts_per_page' => 5,
    'caller_get_posts' => 5,
    'paged' => $paged,
    'meta_key' => 'order-date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    'meta_query' => array(
    	array(
    	'key' => 'order-date',
    	'value' => $value,
    	'compare' => '>=',
    	'type' => 'CHAR'
    	)
    	)
    ));
    if (have_posts()) :
    while (have_posts()) : the_post();
    ?>
  5. Ray Gulick
    Member
    Posted 3 years ago #

    The above code was really close. Here's what finally worked:

    <?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $today = date('Y-m-d');
    query_posts(array(
    	'post_type' => 'performance',
    	'posts_per_page' => 5,
    	'caller_get_posts' => 5,
    	'paged' => $paged,
    	'meta_key' => 'order-date',
    	'orderby' => 'meta_value',
    	'order' => 'ASC',
    	     'meta_query' => array(
    		array(
    		'key' => 'order-date',
    		'meta-value' => $value,
    		'value' => $today,
    		'compare' => '>=',
    		'type' => 'CHAR'
    		)
    	)
    ));
    if (have_posts()) :
    while (have_posts()) : the_post();
    ?>

    Note that $today was defined, then added as a value for comparison to the meta-value from the custom field "order-date", which was also in Y-m-d format.

  6. DarioDN
    Member
    Posted 3 years ago #

    Basing on your script, how can I do to load 12 posts and paginate them in three column?

  7. Ray Gulick
    Member
    Posted 3 years ago #

    For 12 posts per page, change these numbers to 12:
    'posts_per_page' => 5,
    'caller_get_posts' => 5,

    For placing the info in 3 columns, you're going to need to dig into your CSS.

  8. DarioDN
    Member
    Posted 3 years ago #

    mmm, I cannot use CSS (because the column property is supported only by recent browserd). I think can be useful use some kind of "foreach" or "for" cycle... any suggest?
    tnx

  9. Ray Gulick
    Member
    Posted 3 years ago #

    You could create 3 separate queries with offsets that show, in turn, most recent 4, next 4, and next 4 after that (not sure how to implement that; beyond my expertise).

  10. DarioDN
    Member
    Posted 3 years ago #

    I did it but it show an empty value when the post is <=$today.
    Ie:

    1 COL | 2 COL | 3 COL
    POST POST EMPTY
    POST POST POST

    I need that the last post go to the top of third column (or, if is empty is in second, the first post of 3rd goes to second post of 2nd)

  11. Ray Gulick
    Member
    Posted 3 years ago #

    Can you provide a link to the page?

  12. melissaaggie98@gmail.com
    Member
    Posted 2 years ago #

    In a related topic, I was hoping that someone could lend a hand in helping me to solve a problem with a widget I'm using. I simply want to sort the posts that show up in this widget by a custom field "enddate". But for the life of me, I can't figure out what I need to add and where. Can someone please help?

    This is the widget's code:

    [ Please only paste up to ten lines of code. Otherwise use pastebin ]

Topic Closed

This topic has been closed to new replies.

About this Topic