0

I need to select posts to display based on two custom fields with a specific key and order them by a third. THis is what I have, but it is not ordering correctly.

    <?php
$args           = array(
    'post_type' => 'event',
    'order' => 'ASC',
    'meta_query' => array(
        'key' => 'Date Ordering Format',
        'orderby' => 'meta_value',
        'relation' => 'AND',
        array(
            'key' => 'Event Type',
            'value' => '4. take-out tuesday',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'Past Event',
            'value' => 'Past',
            'compare' => 'EXISTS'
        )
    )
);
$featured_query = new WP_Query($args);
if ($featured_query->have_posts()):
    while ($featured_query->have_posts()):
        $featured_query->the_post();
        echo '<div class="fullWidthSeason">';
        echo '<div class="pastEventImage">';
        if (class_exists('MultiPostThumbnails')):
            MultiPostThumbnails::the_post_thumbnail(get_post_type(), 'series-image', $post->ID, 'full', NULL, FALSE);
        endif;
        echo '</div>';
        echo '<div class="featuredBlockDate ' . get_post_meta($post->ID, 'Event Text Left or Right', true) . ' ' . get_post_meta($post->ID, 'Black Text', true) . ' ' . get_post_meta($post->ID, 'Black Text', true) . '">';
        echo 'Past Event: ';
        echo get_post_meta($post->ID, 'Event Date', true);
        echo '</div>';
        echo '<div class="featuredBlockTitle  ' . get_post_meta($post->ID, 'Event Text Left or Right', true) . ' ' . get_post_meta($post->ID, 'Black Text', true) . '">';
        echo get_post_meta($post->ID, 'Season Page Title', true);
        echo '</div>';
        echo '<div class="featuredBlockExcerpt  ' . get_post_meta($post->ID, 'Event Text Left or Right', true) . ' ' . get_post_meta($post->ID, 'Black Text', true) . '">';
        echo the_excerpt();
        echo '</div>';
        echo '<div class="moreButton  ' . get_post_meta($post->ID, 'Event Text Left or Right', true) . '">';
        echo '<div class="moreButtonSeason  ' . get_post_meta($post->ID, 'Event Text Left or Right', true) . '"><a href="' . get_post_meta($post->ID, 'Event RSVP Link', true) . '" target="_blank">' . get_post_meta($post->ID, 'Event RSVP Link Text', true) . '</a></div>';
        echo '</div>';
        echo '</div>';
    endwhile;
    wp_reset_query();
endif;
?>
1
  • Can you indent your code? It's very difficult to parse. It even looks like a part of the array at the beginning has been mixed up but it's hidden because the indenting is broken
    – Tom J Nowell
    Commented Aug 7, 2017 at 2:17

1 Answer 1

0

I think the orderby key is at the wrong place. It think it should be like this:

$args           = array(
    'post_type' => 'event',
    'order' => 'ASC',
    'orderby' => 'meta_value',
    'meta_key' => 'your key to order',
    'meta_query' => array(
        'key' => 'Date Ordering Format',
        'relation' => 'AND',
        array(
            'key' => 'Event Type',
            'value' => '4. take-out tuesday',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'Past Event',
            'value' => 'Past',
            'compare' => 'EXISTS'
        )
    )
);
1
  • This doesn't filter correctly, it is outputting all post type 'event' that are not 'Past'. It is not considering the 'Event Type'.
    – user100754
    Commented Aug 7, 2017 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.