Skip Navigation

[Resolved] How to order per dates

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by fred-r.M 3 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#2084371

Tell us what you are trying to do? On this link (hidden link) I have either on the drop down or tour dates tab some dates I like to order...

For one part I use this:

/* function get_featured_dates with shortcode show_featured_dates to show the dates on a single tour */
function get_my_featured_dates() {
  
  global $post;
  $str = '';
  $origin_id = $post->ID;
  $relationship_slug = 'tourid';
  
  $results = toolset_get_related_posts ( 
        // get posts related to this one
        $origin_id, // get posts connected to this one
        $relationship_slug, // in this relationship
        array(
        	'query_by_role' => 'parent', // origin post role
        	'role_to_return' => 'child', // role of posts to return
        	'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
        	'limit' => 999, // max number of results
        	'offset' => 0, // starting from
			'orderby' => 'wpcf-tour-date-field',
			'order' => 'ASC',
        	'args' => null // for adding meta queries etc.
    	    )
        );
  
  $alltourdates = count($results);
  $today = strtotime('today midnight');
    
		$mycounter = 0;
			while ($mycounter < $alltourdates)
			{
			$tour_date_field = get_post_meta( $results[$mycounter], 'wpcf-tour-date-field', true);
			if($today < $tour_date_field) {
				$myresults = date( 'd. F Y', $tour_date_field);
				$str .= $myresults.'<br>';
			}
			$mycounter++;
 
			}
		return $str;
}	
add_shortcode( 'show_my_featured_dates', 'get_my_featured_dates' );

I am not sure if this orderby is working or not. Or how I need to use it.

Is there any documentation that you are following? https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post_types

Is there a similar example that we can see?

What is the link to your site? hidden link

#2084709

Hi,

Thank you for contacting us and I'd be happy to assist.

In my tests, I was able to order the results correctly based on the "tour-date-field", by using slightly updated code:


/* function get_featured_dates with shortcode show_featured_dates to show the dates on a single tour */
function get_my_featured_dates() {

	global $post;
	$str = '';
	$origin_id = $post->ID;
	$relationship_slug = 'tourid';

	$results = toolset_get_related_posts ( 
		// get posts related to this one
		$origin_id, // get posts connected to this one
		$relationship_slug, // in this relationship
		array(
			'query_by_role' => 'parent', // origin post role
			'role_to_return' => 'child', // role of posts to return
			'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
			'limit' => 999, // max number of results
			'offset' => 0, // starting from
			'orderby' => 'meta_value_num',
			'order' => 'ASC',
			'args' => array(
				'meta_key'	=> 'wpcf-tour-date-field',
			),
		)
	);

	$alltourdates = count($results);
	$today = strtotime('today midnight');

	$mycounter = 0;
	while ($mycounter < $alltourdates)
	{
		$tour_date_field = get_post_meta( $results[$mycounter], 'wpcf-tour-date-field', true);
		if($today < $tour_date_field) {
			$myresults = date( 'd. F Y', $tour_date_field);
			$str .= $myresults.'<br>';
		}
		$mycounter++;
	}
	return $str;
}
add_shortcode( 'show_my_featured_dates', 'get_my_featured_dates' );

Please note, how I've set "orderby" to "meta_value_num" and passed the target meta_key through the 'args' array.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )

regards,
Waqar

#2084765

My issue is resolved now. Thank you!