Viewing 18 posts - 1 through 18 (of 18 total)
  • Author
    Posts
  • #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.

    #1417298

    Hey 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.

    // https://kriesi.at/support/topic/using-blog-element-for-events-calendar-listings-need-to-order-by-event-date/#post-1405483

    Best regards,
    Ismael

    #1417308

    Hi 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/29trzjkx

    We are not using Events Calendar plugin. So please give a detailed answer how can we achieve the goal we have asked here.

    #1417377

    Hello Ismael,

    No response from you end, waiting for your feedback and to solve my issue.

    #1417394

    Hi,

    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-1415662

    Best regards,
    Ismael

    #1417689

    Hi Ismael,

    Not solved, please help to fix this.

    #1417839

    Hi Support,

    Is thee anyone to look into and solve my issue??

    #1418014

    Hey!

    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!
    Ismael

    #1418017

    Hi 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.

    #1418021

    it 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 )

    #1418024

    Hi 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’);
    Screenshot-from-2023-09-04-15-33-21
    Screenshot-from-2023-09-04-15-33-56

    If you want i can give ftp and admin credentials of our staging site.

    #1418026

    did 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

    #1418038

    Sorry 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-vacations

    #1418120

    Hi 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 – maritimetravel2023

    #1418130

    Hey!

    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!
    Ismael

    #1418135

    Hi 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.

    #1418150

    you see that the if clause is allready an array – so comma separated id’s will do that job:

    …
    if ( is_page(array(1132, 6481)) ) {
    
    #1418234

    Hi,

    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

Viewing 18 posts - 1 through 18 (of 18 total)
  • You must be logged in to reply to this topic.