Support

Account

Home Forums General Issues using both published date and date picker to sort posts

Solved

using both published date and date picker to sort posts

  • Hi,

    I would like to sort posts using firstly the custom field date picker by ACF plugin for wordpress, and then when that’s non existing or empty, by the published date.

    Our theme works with cases so I cooked up something like this but I failed misearbly, any suggestions ?

    The reason for this is because we are currently uploading a lot of old concert reports, so published date should be kept on today (the easiest given the vast amount of data yet to be uploaded), where the concert date is to be set manually.

    The ACF field “datum” is then later used in the title of the posts to help generalize the way dates are displayed.

                case 'concertdatum':
             $wp_query_args['meta_query'] = 	array(        
            'relation' => 'AND',
            'datum_clause' => array(
                'meta_key' => 'datum',
                'meta_type'	=> 'DATETIME'
            ),
            'publish_clause' => array(
                'column' => 'post_date_gmt',
                'compare' => 'EXISTS',
            ), 
        )
        $wp_query_args['order'] = array(
            'datum_clause' => 'DESC',
            'publish_clause' => 'DESC'
        );
    
                
                break;

    A working bit of code, sorting only by the custom field looks like this:

                case 'concertdatum': 
                    $wp_query_args['meta_key'] = 'datum';
                    $wp_query_args['meta_type'] = 'DATETIME';            
                    $wp_query_args['orderby'] = 'meta_value';
                    $wp_query_args['order'] = 'DESC';
                    break;  
  • Should

    $wp_query_args['order']

    not be

    $wp_query_args['orderby']

    ?

  • post_date_gmt is not a meta field. Remove it from your meta query.
    WP does not have an option to order posts by the post_date_gmt value, only date or “post_date”

    Also, ACF does not store date fields as a date. Use number of string, and you need to set a compare or value

    
    
            'datum_clause' => array(
                'meta_key' => 'datum',
                'compare' => 'EXISTS'
            ),
    

    Then in your order by use

    
    
        $wp_query_args['orderby'] = array(
            'datum_clause' => 'DESC',
            'date' => 'DESC'
        );
    
  • Currently I have this but it still puts the published date first, unless I misunderstood you and did something wrong of course…

    case 'concertdatum2':
             $wp_query_args['meta_query'] = array(        
                                                  'datum_clause' => array('meta_key' => 'datum', 'compare' => 'EXISTS')
      																				   );
      		  $wp_query_args['orderby'] = array('datum_clause' => 'DESC', 'date' => 'DESC');
                break;
  • As far as I know it should be working.

    It should be ordering the posts by the custom date field. If two of those fields have the same date then those should be ordered according to the post date.

    You also have to remember that an ACF date field does not store a time. So today would be in the DB like this 20181211 Two posts that have today’s date in the custom field will be ordered by the post date, which includes a timestamp, for example 2018-12-11 10:34:22

  • Ah, so if the custom field is a date time field, which upon closer look it is, I need to alter the code probably ?

  • turns out it needed to be post_date instead of simply date, this is the working code.

                case 'concertdatum':
                   $wp_query_args['meta_query'] = array(        
                                                        'datum_clause' => array('key' => 'datum','compare' => 'EXISTS')
                                                       );
                  $wp_query_args['orderby'] = array('datum_clause' => 'DESC', 'post_date' => 'DESC');
                	break;

    For some odd reason however, the mega menu that my theme uses refuses to sort the posts like I request it to, but that’s a different question not for this site 😉

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘using both published date and date picker to sort posts’ is closed to new replies.