-
AuthorPosts
-
August 25, 2023 at 4:07 pm #1417154
Hi Support,
I also want to sort custom post type posts with custom post meta field
In the above link you can see posts are showing as October 8 – 20, 2023 in ascending order but these posts are not showing as per dates which you are viewing rather posts are showing as per their created/modified i.e we have changed the created/modified date as events date when this will happen. For example Magical Maritimes Fall Foliage Tour will commence on 8th Oct, 2023 so we have create/modified the date of that posts to 8th Oct,2023.
August 28, 2023 at 6:37 am #1417298Hey NicomIT,
Thank you for the inquiry.
Are you using a plugin to create the custom post type? Unfortunately, this feature is not available by default. However, if you’re using the Events Calendar plugin to create events and the Blog Posts element to display the items, you can use the following filter to adjust the sorting of the items based on the start date.
Best regards,
IsmaelAugust 28, 2023 at 9:51 am #1417308Hi Ismael,
Thanks for your reply. But I have used Custom Post UI to create custom post type named “Escorted Vacations”. In the below given link you can see how we have implemented those posts.
https://tinyurl.com/29trzjkxWe are not using Events Calendar plugin. So please give a detailed answer how can we achieve the goal we have asked here.
August 29, 2023 at 8:58 am #1417377Hello Ismael,
No response from you end, waiting for your feedback and to solve my issue.
August 29, 2023 at 10:33 am #1417394Hi,
Thank you for the update.
How did you define or set the start date? Did you use a custom field? It is possible to sort the items based on a meta or custom field. You can learn more about it in the following link. Look for the usage examples of meta_value under the Order & Orderby Parameters section.
// https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
You can also see a few examples in the following threads.
// https://kriesi.at/support/topic/change-the-sort-order-of-masonry-elements/#post-1412022
// https://kriesi.at/support/topic/change-view-out-of-stock-article/#post-1366302
// https://kriesi.at/support/topic/sort-articles-with-acf-field/#post-1415662Best regards,
IsmaelAugust 31, 2023 at 12:30 pm #1417689Hi Ismael,
Not solved, please help to fix this.
September 1, 2023 at 4:11 pm #1417839Hi Support,
Is thee anyone to look into and solve my issue??
September 4, 2023 at 9:36 am #1418014Hey!
What is the name of the custom field that you’re using for the event start date? We may need to access the site in order to further check the issue. Please provide the login details in the private field.
As suggested above, you have to use the query filter and set the orderby parameter to meta_value or meta_value_num. The value of the meta_key should be the name of the start date custom field.
$query['meta_key'] = 'start_date'; $query['orderby'] = 'meta_value_num'; $query['order'] = 'ASC';
Cheers!
IsmaelSeptember 4, 2023 at 9:52 am #1418017Hi Support,
journey_start_date & journey_end_date are the two custom post meta fields that we have created for CPT escorted-vacations and we want to displat the posts in ascending order with orderby journey_start_date, can you please help me regarding this.
September 4, 2023 at 10:23 am #1418021it is hard to give advice without a working test environment to check.
Can you try :
function pre_sort_filter_escorted_vacations($query) { if(!is_admin() && $query->is_main_query()) { $post_type = $wp_query->query['post_type']; if ( $post_type == 'escorted-vacations') { $query->set('meta_key', 'start_date'); $query->set('orderby', 'meta_value_num'); $query->set('order', 'ASC'); } } } add_action('pre_get_posts', 'pre_sort_filter_escorted_vacations');
do not know if this works – then if it works on the loop ( i don’t think so )
September 4, 2023 at 12:21 pm #1418024Hi Guenni007,
Thanks for your reply.
I have tried your code but it’s not reflecting the correct order as it should be. If you check the below link with the screenshots, you will find that i have already created two custom post meta fields with the help of ACF plugin, named journey_start_date & journey_end_date. Below is the modified code of yours that i have implemented.
add_filter( ‘posts_distinct’, ‘cf_search_distinct’ );
function pre_sort_filter_escorted_vacations($query) {
if(!is_admin() && $query->is_main_query()) {
$post_type = $wp_query->query[‘post_type’];
if ( $post_type == ‘escorted-vacations’) {
$query->set(‘meta_key’, ‘journey_start_date’);
$query->set(‘orderby’, ‘meta_value_num’);
$query->set(‘order’, ‘ASC’);
}
}
}
add_action(‘pre_get_posts’, ‘pre_sort_filter_escorted_vacations’);
If you want i can give ftp and admin credentials of our staging site.
September 4, 2023 at 12:52 pm #1418026did you adjust the code to your ACF ?
as I said above, it is likely that my code does not work in the loop. – I’ll have to call in someone with more programming skills – sorry.
by the way: please use the code tag to post snippets . it is hard to read ( and a lot of signs are changed ) if you do not.
Next: your links above goes to a different url – the one in your screenshot is password portected
Did you look to that page logged out ? because the above if-clause includes the if not admin
September 4, 2023 at 3:02 pm #1418038Sorry Guenni007,
I forgot to give you the password as the staging site is password protected to prevent site from indexing to google. The password is maritimetravel2023. I have checked those pages with logged in condition.
staging site url: https://www.maritimetravel.nicomitcms.com/custom-escorted-vacationsSeptember 5, 2023 at 8:02 am #1418120Hi Ismael & Guenni007,
All the details given in private content, please go through and let me know how to solve my issue
https://www.maritimetravel.nicomitcms.com/wp-login.php
site password without admin login – maritimetravel2023September 5, 2023 at 9:08 am #1418130Hey!
Thank you for the info.
We made some modifications to the code in the functions.php file, and it’s now functioning as intended. However, we had to change the format of the journey_start_date from “mm/dd/yyyy” to “yyyymmdd,” which is the default format recognized by WordPress.
To test these changes, we adjusted the journey_start_date for the posts “Shades of Ireland,” “Magdalen Islands & Gaspé Explorer,” and “Newfoundland & Labrador Discovery.” You can verify this by changing the “order” parameter from “ASC” to “DESC.” When you do this, you’ll notice that the mentioned posts will move to the very bottom of the list in their intended order.
add_filter('avia_post_slide_query', 'avia_post_slide_query_mod'); function avia_post_slide_query_mod( $query ) { if ( is_page(array(1132)) ) { $query['meta_key'] = 'journey_start_date'; $query['orderby'] = 'meta_value'; $query['order'] = 'ASC'; } return $query; }
All you need to do now is adjust the format of the event date.
Cheers!
IsmaelSeptember 5, 2023 at 9:41 am #1418135Hi Ismael,
Thanks for looking into this. in the code you have used is_paged function and in it you have used page id but the problem is that this site is in multilanguage with 2 sites in english and 1 in french and every page have different ids, so how to implement your code for each language. Secondly even if i used your way then i have to put 9 page ids and in future if same functionality is implemented in few other pages then I have to put the page ids of those pages in the given above function.
September 5, 2023 at 1:02 pm #1418150you see that the if clause is allready an array – so comma separated id’s will do that job:
… if ( is_page(array(1132, 6481)) ) {
September 6, 2023 at 5:53 am #1418234Hi,
Secondly even if i used your way then i have to put 9 page ids and in future if same functionality is implemented in few other pages then I have to put the page ids of those pages in the given above function.
Instead of using the is_page function, we can also check if the element queries for the post type “escorted-vacations”. This way, you won’t have to update the IDs in the array if another events page is created.
if ( in_array('escorted-vacations', $query['post_type']) ) {
Best regards,
Ismael -
AuthorPosts
- You must be logged in to reply to this topic.