orderby date

Support General orderby date

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #4731
    pepeinteractivepepeinteractive
    Participant

    I Created a data field Thus:

    add_filter( 'rwmb_meta_boxes', 'register_meta_boxes' );
    function register_meta_boxes( $meta_boxes ) {
    	$meta_boxes[] = array(
    		'title' => __( 'Data corso/evento'),
            'post_types' => 'product',
    		'fields' => array(
    			array(
    				'name' => __( 'Giorno evento' ),
    				'id'   => 'day_event',
    				'type' => 'date',
    				'js_options' => array(
    					'autoSize'        => true,
    					'buttonText'      => __( 'Select Date' ),
                        "dateFormat" => "dd/mm/yy",
    					'numberOfMonths'  => 1,
    					'showButtonPanel' => true,
    				),
    				'inline' => true,
    			)
    		),
            
    	);
    	return $meta_boxes;
    }
    
    and placed in my shortcode:
    
        $options = array(
    				'post_type' => $post_type,
    				'product_cat' => $product_cat,
    				'posts_per_page' => $posts_per_page,
                    'meta_key' => 'day_event',
                    'orderby' => 'meta_value',
                    'order' => 'ASC',
                
        );

    How can I do that reorder by date?
    thank you!

    #4736
    Anh TranAnh Tran
    Keymaster

    You query is correct. However, the problem here is the value you saved for date has the format "dd/mm/yyyy" which will cause the incorrect order when sorting. I suggest you use "yyyy-mm-dd" format, which is safe when ordering.

    #4737
    pepeinteractivepepeinteractive
    Participant

    ok, thanks,
    but if I want to display it as "dd/mm/yyyy"?

    #4738
    Anh TranAnh Tran
    Keymaster

    If so, you can set the timestamp parameter for the field true. This will make the field save the timestamp instead of date string, so you can sort by that, while still display in the correct format to users.

    You can see the docs here.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.