Custom Fields
Re-Envisioned
Support General orderby date

This topic contains 3 replies, has 2 voices, and was last updated by  Anh Tran 2 years, 7 months ago.

  • Creator
    Topic
  • #4731

    pepeinteractive
    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #4736

    Anh 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

    pepeinteractive
    Participant

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

    #4738

    Anh 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 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.