Support

Account

Home Forums Front-end Issues how to get sort post by date picker date

Solved

how to get sort post by date picker date

  • i am using date picker for event post. it very simple. i can get date but i never sort that post date wise . can anyone help me out please . bellow is my code

     <?php $news = new WP_Query('category_name=events&orderby=date&showposts=2'); ?>
    <?php while ($news->have_posts()) : $news->the_post(); ?>
    <li><h3> <?php the_field('date'); ?></h3><h4><?php the_title(); ?></h4></li>
    <?php endwhile; ?>

    my event post category name events.
    my date picker name date

    i can get date but i want to short by datepicker date. events&orderby=date& this only work for wordpress date.

    i tired `

    <?php $posts = new WP_Query(‘category_name=events&showposts=2’); ?>
    <?php

    $posts = get_posts(array(
    ‘post_type’ => ‘events’,
    ‘posts_per_page’ => -1,
    ‘meta_key’ => ‘date’,
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘DESC’
    ));

    if( $posts ): ?>

    <ul>

    <?php foreach( $posts as $post ):

    setup_postdata( $post )

    ?>
    <li>
    <a href=”<?php the_permalink(); ?>”><?php the_title(); ?> (date: <?php the_field(‘date’); ?>)</a>
    </li>

    <?php endforeach; ?>

    </ul>

    <?php wp_reset_postdata(); ?>

    <?php endif; ?>

    `

    this also not work

  • Hi @pagol

    You need to check out the orderby parameter of wp_query.
    I haven’t tested this code and you might need to set the order parameter to either ASC or DESC as well. But give it a go:

    
    <?php 
    $args = array(
    	'cat' => 'event',
    	'posts_per_page' => 2,
    	'orderby' => 'meta_value',
    	'meta_type' => 'DATE',
    	'meta_key' => 'fieldnameofdatepicker'
    	
    );
    $news = new WP_Query($args); 
    ?>
    <?php while ($news->have_posts()) : $news->the_post(); ?>
    	<li>
    		<h3><?php the_field('date'); ?></h3>
    		<h4><?php the_title(); ?></h4>
    	</li>
    <?php endwhile; ?>
    
  • thank u so much @jonathan … big thanks. it is perfectly work. i also paste working and test coding bellow

    <?php 
    $args = array(
    	'cat' => 'events',
    	'posts_per_page' => 2,
    	'orderby' => 'meta_value_num',
    	'order'	=> 'ASC',
    	'meta_type' => 'DATE',
    	'meta_key' => 'date'
    	
    );
    $news = new WP_Query($args); 
    ?>
    <?php while ($news->have_posts()) : $news->the_post(); ?>
    	<li>
    		<h3><?php the_field('date'); ?></h3>
    		<h4><?php the_title(); ?></h4>
    	</li>
    <?php endwhile; ?>
  • Hi @pagol

    Great! It’s always a bit “risky” when posting code not tested but it seems it worked out this time 😉

    Good luck with your project and don’t hesitate to post here again if you need to.

  • @jonathan thanks 🙂 i made another also in PRO section

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘how to get sort post by date picker date’ is closed to new replies.