Support

Account

Home Forums Front-end Issues Displaying based on multiple date fields

Solving

Displaying based on multiple date fields

  • anyone have thoughts on how I would go about making this work. The problem is there are 2 date fields that could be in use. So I want to see if one is in the range and use that if not use the other. Can’t seem to find any documentation that has any help.

    $args = array(
    if ( ($sale_closing_date = get_field(‘presale_closing_date’, $post_id) ) && ( $sale_closing_date < $today ) ) {
    $sale_opening_date = get_field(‘presale_opening_date’, $post_id);
    } else {
    $sale_closing_date = get_field(‘sale_closing_date’, $post_id);
    $sale_opening_date = get_field(‘sale_opening_date’, $post_id);
    }
    ‘post_type’ => ‘tv_flat_screens’,
    ‘meta_key’ => $sale_closing_date,
    ‘posts_per_page’ => -1,
    ‘orderby’ => ‘meta_value_num’,
    ‘order’ => ‘ASC’,
    ‘meta_query’=> array(
    ‘key’ => $sale_closing_date,
    ‘compare’ => ‘<‘,
    ‘value’ => $today
    ),
    );
    $IOC_content_closed = get_posts( $args );

  • I know I am new here but still have not solved this. My guess it to make a unified date field and use that?

    There are lots of ideas going through my head but none seem to actually make it work as expected.

  • I looked at this when you first posted, but I don’t really have an answer. It looks like you are trying to order the posts by two different fields as if they are one field. You can do a query to make sure that one of the fields is in the correct range, but you can’t order them by the values in these field….. well, you can, but not at the same time. For example, you could order them by the “end date” and then order them by the “start date”, or you can order them by “end date 1” and then by “end date 2”. Hopefully I’ve explained this well enough that you see the problem.

    If you need to order by both of these fields then your best bet is to do what you suggest. Create an acf/save_post filter that looks at the date fields and takes the one you want to sort by and puts them into another custom meta field using update_post_meta() and then order the posts by this single field.

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

The topic ‘Displaying based on multiple date fields’ is closed to new replies.