WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Change order of posts and exclude some posts (10 posts)

  1. thorbj
    Member
    Posted 3 years ago #

    Hi!
    I have made a custom template for a wordpress page. This template lists all posts in a certain category.
    I want to change the order of the post, so they are listed ascending by a custom field named date. The date in the custom field is formatted like this: "d/m/Y". I also want to exclude posts where the date has passed.

    The code int he template looks like this:

    [code moderated per Forum Rules - please use the Pastebin]

    http://pastebin.com/cJJhF2Mi

    I hope somebody can help me with this.
    Thanks in advance!

  2. jobmeer
    Member
    Posted 3 years ago #

    For the order of the post basen on a custom field use
    meta_key=CustomFieldName&orderby=meta_value In your query.

  3. thorbj
    Member
    Posted 3 years ago #

    Thanks for the response.
    How do I use this in my query?
    When I replace <?php query_posts('category_name=events'); ?> on line 29 with <?php query_posts('meta_key=Dato&orderby=meta_value'); ?>, i break the code.

    Thanks in advance.

  4. jobmeer
    Member
    Posted 3 years ago #

    Try something like this

    <?php if(have_posts()) : ?>
    <?php $loop = new WP_Query( array(
    'posts_per_page' => 5,
    'meta_key' => 'Dato',
    'orderby' => 'meta_value',
    'order' => 'ASC') );
    while ( $loop->have_posts() ) : $loop->the_post();?>
  5. thorbj
    Member
    Posted 3 years ago #

    Thanks again :)
    But I still can't figure this out. Now, when I replace <?php if (have_posts()) : while (have_posts()) : the_post(); ?> with your code, it only returns a blank page. It looks as it can't find any posts to display.
    What am I doing wrong here?

  6. jobmeer
    Member
    Posted 3 years ago #

    Is 'Dato' the right custom field name? it's case sensitive!

    You have to add also a array for filter the right category:

    <?php if(have_posts()) : ?>
    <?php $loop = new WP_Query( array(
    'posts_per_page' => 5,
    'cat'      => 22,
    'meta_key' => 'Dato',
    'orderby' => 'meta_value',
    'order' => 'ASC') );
    while ( $loop->have_posts() ) : $loop->the_post();?>

    Also have a look at:
    http://wordpress.org/support/topic/ordering-by-meta-key-value?replies=3

  7. jobmeer
    Member
    Posted 3 years ago #

  8. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    http://www.wpmods.com/use-meta_query-query-posts-postmeta

    Has some info about sorting using post meta as well, some new options that came with WP 3.1

  9. kamportal
    Member
    Posted 3 years ago #

    wow 8)

  10. thorbj
    Member
    Posted 3 years ago #

    Ok, thanks! I think I got it now :)
    I ended up with this code:

    <?php if(have_posts()) : ?>
    <?php $loop = new WP_Query( array(
    'posts_per_page' => 10,
    'cat'      => 15,
    'meta_key' => 'Dato',
    'orderby' => 'STR_TO_DATE(meta_value, \'%d/%m/%Y\')',
    'order' => 'ASC') );
    while ( $loop->have_posts() ) : $loop->the_post();

    Also I added the following, to exclude expired posts:

    $expirationtime = get_post_custom_values('expiration');
    	if (is_array($expirationtime)) {
    	$expirestring = implode($expirationtime);
    	}
    
    $secondsbetween = strtotime($expirestring)-time();
    if ( $secondsbetween > 0 ) {

    I had to add another custom field called expiration, populated with mm/dd/yyyy 00:00:00, for this to work.

    Again, thanks alot :)

Topic Closed

This topic has been closed to new replies.

About this Topic