Skip Navigation

[Resolved] Display child posts by custom date field

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to Order child posts by “Custom Date Field” while having many-to-many relationships for custom post types?

Solution:
Orderby Custom Field works fine for normal posts or custom posts (non-relational posts). But the same solution is not possible with built-in options of Views for many-to-many relational data, so the orderby Custom date field and query filtering is being done by the following code. Please note that you will need to modify this code to add your post types, custom fields and other data to make it work for your site or setup:

I have created a custom shortcode [display_related_session_posts] and added following code in theme’s functions.php file and now the order is working good:

function display_parent_post_order_by_date_field( $atts, $content = '' ) {
     
     extract( shortcode_atts( array(
        'speaker_id' => '',
    ), $atts ) );
  
    $child_args = array(
        'post_type' => 'speaking-slot',
        'numberposts' => -1,
        'order' => 'ASC',
        'meta_query' => array(array('key' => '_wpcf_belongs_speaker_id', 'value' => $speaker_id))
    );
    $child_posts = get_posts($child_args);
     
    $parent_ids = array();
    foreach($child_posts as $child) { 
        $parent_ids[] = get_post_meta($child->ID, '_wpcf_belongs_session_id', true);
    }
     
    $session_args = array(
        'post_type' => 'session',
        'posts_per_page' => -1,
        'meta_key' => 'wpcf-start-date-time',
        'orderby' => 'meta_value',
        'order' => 'ASC', // We can change order from here ASC/DESC
        'post__in' => $parent_ids,  
    );
     
    $the_query = new WP_Query( $session_args );
     
    // The Loop
     
    $post_contents = '';
    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
          $post_contents .= do_shortcode($content);
        endwhile;
    endif;
    // Reset Post Data
    wp_reset_postdata();
     
    return $post_contents;
     
}
add_shortcode( 'display_related_session_posts', 'display_parent_post_order_by_date_field' );
This support ticket is created 6 years, 10 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

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

This topic contains 18 replies, has 2 voices.

Last updated by Noman 6 years, 10 months ago.

Assisted by: Noman.

Author
Posts
#538806

I needed to create a many-to-many relationships. So I have the following CPT:
1. Speaker
2. Session
3. Speaking slot (intermediary between first 2 CPT)

I followed the guide on this page (https://toolset.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/) and got everything to work, except:

On the page of a given speaker, where I want to display the list of sessions he is giving speeches in, I needed the session views to be sorted by date (custom field in Session CPT). Somehow I wasn't able to do so. I hope there is a way to achieve that?

#539068

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Dean,

Thank you for contacting Toolset support.

We have other clients who had the similar issue and was also able to solve it, this may help in your case, the known solutions mentioned here:
https://toolset.com/forums/topic/sort-posts-by-custom-date/

Note: In the code provided in above thread please replace “when” with your Custom Post Type Field name.

Thank you

#539608

Thank you Noman for your response. The page you refered to did not solve my problem. I guess you misunderstood my question too.

Let me ask in different way...

If we followed the guides on this page:
https://toolset.com/documentation/toolset-training-course/part-9-many-to-many-relationships-in-toolset/
Under the heading where it says: Creating a Single Artist Page — how do we sort the associated event posts by date (custom field)?

#539658

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

order by date field.png

Hello Dean,

Okay, now I understand. You can order post by custom date field form Ordering option in a View.
Please go to: Toolset >> Edit any View >> Ordering >> Custom date field (see attached screenshot)

Thank you

#539683

> You can order post by custom date field form Ordering option in a View. Please go to: Toolset >> Edit any View >> Ordering >> Custom date field

Before I submit this request, I have already tried that. It returned with no results.

It's worth mentioning that under Loop Output, my code is as follows:

[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->

    <wpv-loop>
    <div>

  • [wpv-post-link id="$session"]
  • </div>
    </wpv-loop>

<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
[wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]

#539688

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Dean,

To further debug this issue, I need to request temporary access (WP-Admin and FTP Login info) to your staging site.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Links to the [View] Edit Screen and [Page] Edit Screen where you have inserted this view.

✙ What is the custom date field name?

✙ What is the expected output? In which order you want to display posts?

Looking forward to help you.
Thank you

#539835

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you sending login info and more details. I am working on it right now and will get back to you soon with an update.

Thank you for your cooperation.

#539916

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

I have been working on it, suddenly there is an error coming from Views and I am not able to log into WP Admin dashboard. FTP login info is not working, please recheck and provide me with working FTP.

You can also rename or delete Views plugin using FTP or filemanager in the cPanel, Views plugin is located in this folder: wp-content/plugins -- this way WP admin will work again and I can continue working.

Thank you for your co-operation.

#539941

Hi Noman, I have disabled Views plugin by renaming the plugin folder. The dashboard is accessible again.

#540081

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Dean, Thanks for making the change. It got working and I was close to the solution and suddenly its gone again, not sure how this is happening may be something wrong between my internet connection and CDN which is failing in the mid.

Can you please provide working FTP login info, that will speedup the work, looks like username / password is not working or there is timeout.

Further, I would like to add that the order by Custom Field Date is working fine on normal Views but looks like it is not expected to get working on many-to-many relationship. I am working on a workaround for this. After I get to WP-admin I will take duplicator backup package, install it on localhost and develop solution over there. I have enabled private fields for next reply.

Sorry for the back and forth, thanks so much for your patience.

#540152

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Great, thanks I am able to connect now to FTP using port 2222. Working further on it now.

#540318

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Dean,

I have created a custom shortcode [display_related_session_posts] and added following code in theme’s functions.php file and now the order is working good:
hidden link

This solution was not possible with built-in options of View for many-to-many relational data, so the order and query filtering is being done by this code:

function display_parent_post_order_by_date_field( $atts, $content = '' ) {
	
	 extract( shortcode_atts( array(
        'speaker_id' => '',
    ), $atts ) );
 
	$child_args = array(
		'post_type' => 'speaking-slot',
		'numberposts' => -1,
		'order' => 'ASC',
		'meta_query' => array(array('key' => '_wpcf_belongs_speaker_id', 'value' => $speaker_id))
	);
	$child_posts = get_posts($child_args);
	
	$parent_ids = array();
	foreach($child_posts as $child) { 
		$parent_ids[] = get_post_meta($child->ID, '_wpcf_belongs_session_id', true);
	}
	
	$session_args = array(
		'post_type' => 'session',
		'posts_per_page' => -1,
		'meta_key' => 'wpcf-start-date-time',
		'orderby' => 'meta_value',
		'order' => 'DESC', // We can change order from here ASC/DESC		
'post__in' => $parent_ids,  
	);
	
	$the_query = new WP_Query( $session_args );
	
	// The Loop
	
	$post_contents = '';
	if ( $the_query->have_posts() ) :
		while ( $the_query->have_posts() ) : $the_query->the_post();
		  $post_contents .= do_shortcode($content);
		endwhile;
	endif;
 
	return $post_contents;
	
	// Reset Post Data
	wp_reset_postdata();
	
	
}
add_shortcode( 'display_related_session_posts', 'display_parent_post_order_by_date_field' );

I hope it helps, Thank you

#540433
Fullscreen_23_06_2017__17_08.png

Noman, instead of simply telling me your product does not support the feature, you have gone above and beyond to get it done for me. I'm nothing but impressed! Tell me where can I post a good review on you, you deserve a raise!

Though, I still need a little help... not sure if it's related....

Take a look at hidden link again, see the body text, it's showing placeholder text. But if you edit the post, you can see we have populated actual content about Jan Melissen. I simply couldn't comprehend what's causing it to show different text. Can you help?

#540446

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for your compliments, I am happy to hear that 🙂 I am going to check the content that why its appearing wrong, will let you know soon. May be its cache or CDN cache.

As for the feedback once you will resolve this ticket you will get an email using that you can leave feedback comments. And if you would like I can also share with you our support manager & leader emails who you can pass any compliments directly about my work.

Thank you

#540472

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Dean,

I have updated the code slightly and its working good now: hidden link

function display_parent_post_order_by_date_field( $atts, $content = '' ) {
	
	 extract( shortcode_atts( array(
        'speaker_id' => '',
    ), $atts ) );
 
	$child_args = array(
		'post_type' => 'speaking-slot',
		'numberposts' => -1,
		'order' => 'ASC',
		'meta_query' => array(array('key' => '_wpcf_belongs_speaker_id', 'value' => $speaker_id))
	);
	$child_posts = get_posts($child_args);
	
	$parent_ids = array();
	foreach($child_posts as $child) { 
		$parent_ids[] = get_post_meta($child->ID, '_wpcf_belongs_session_id', true);
	}
	
	$session_args = array(
		'post_type' => 'session',
		'posts_per_page' => -1,
		'meta_key' => 'wpcf-start-date-time',
		'orderby' => 'meta_value',
		'order' => 'ASC', // We can change order from here ASC/DESC
		'post__in' => $parent_ids,  
	);
	
	$the_query = new WP_Query( $session_args );
	
	// The Loop
	
	$post_contents = '';
	if ( $the_query->have_posts() ) :
		while ( $the_query->have_posts() ) : $the_query->the_post();
		  $post_contents .= do_shortcode($content);
		endwhile;
	endif;
	// Reset Post Data
	wp_reset_postdata();
	
	return $post_contents;
	
}
add_shortcode( 'display_related_session_posts', 'display_parent_post_order_by_date_field' );

Thank you

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.