Tell us what you are trying to do? On this link (enlace oculto) 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? enlace oculto
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
My issue is resolved now. Thank you!