Home Forums Front-end Issues Displaying posts using a custom date field Reply To: Displaying posts using a custom date field

  • Hi John,

    Still working on this and try to understand it line by line.

    Regarding this code that you suggested before:

    function my_pre_get_posts($query=false) {
    if (is_admin() || !$query || !is_a($query, ‘WP_Query’) ||
    !$query->is_main_query()) {

    if (isset($query->query_vars[‘post_type’]) &&
    $query->query_vars[‘post_type’] == ‘post’) {

    $query->set(‘meta_key’, get_field(‘start_date’));
    $query->set(‘meta_type’, ‘DATE’);
    $query->set(‘orderby’, ‘meta_value’);
    $query->set(‘order’, ‘DESC’);
    add_action(‘pre_get_posts’, ‘my_pre_get_posts’);

    1-Is it correct that I used:

    to get the my custom field value? , or I just should use ‘start_date’ ?

    2- I’ve noticed nothing works unless when I remove the following if clause:

    if (isset($query->query_vars[‘post_type’]) &&
    $query->query_vars[‘post_type’] == ‘post’) {

    When I remove the if clause above, code seems to alter the post order, but couldn’t find out based on what?

    3- Inside ACF plugin panel, and regarding that ‘start_date’ custom form value, I’ve set its “save format” as yymmdd, and display format as dd/mm/yy

    Can it be a possibility that sorting doesn’t work because of display format is dd/mm/yy?

    Many Thanks,