[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.
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)
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?
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)
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?
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.
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.
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' );
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?
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.