• Hi, I have a Tour post type in my WordPress installation. In each tour, there is a custom field start_date, so it is like this:

    Tour to Sydney
    – start date: 20 June 2018
    – start date: 25 June 2018
    – start date: 30 June 2018

    Tour Melbourne

    – start date: 22 June 2018
    – start date: 10 August 2018

    Question

    How is the code in wp_query to show like this (sorted by dates):

    Tours:

    – Tour Sydney: 20 June 2018
    – Tour Melbourne: 22 June 2018
    – Tour Sydney: 25 June 2018
    – Tour Sydney: 30 June 2018
    – Tour Melbourne: 10 August 2018

    Thank you for any reply from the community

    • This topic was modified 6 years, 5 months ago by profmustamar.
    • This topic was modified 6 years, 5 months ago by profmustamar.
    • This topic was modified 6 years, 5 months ago by profmustamar.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Try below code.
    However you have to check date format in data base to make below query working.

    $args = array(
            'post_type'        => 'tour',
            'order'            => 'ASC',
            'orderby'          => 'meta_value',
            'posts_per_page'   => -1,
            'meta_query' => array(
              array(
                'key' => 'start_date',
                'value' => date('Y-m-d'),
                'compare' => '>=',
                'type' => 'NUMERIC'
              )
            )
        );
    
        $my_query  = new WP_Query( $args );
    Thread Starter profmustamar

    (@profmustamar)

    Hi, @janak007
    Thank you for your reply. But your code only shows post and sort it by date.
    What I need is to show all dates (and sort it), and also shows the posts.

    For example, in a tour A post there are 3 dates, and tour B there are 2 dates, I want it looked like this:

    Tours:

    – Tour A: 20 June 2018
    – Tour B: 22 June 2018
    – Tour A: 25 June 2018
    – Tour A: 30 June 2018
    – Tour B: 10 August 2018

    Thread Starter profmustamar

    (@profmustamar)

    Anyone?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Query Post with Custom Field Values’ is closed to new replies.