Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.



I am not sure whether this is an Advanced Custom Fields-related question, or a general PHP question, so I have also posted this to the ACF support forum, for those of you who are also looking for similar help.

I'm currently working on a local directory website, focussed specifically at it nightlife. I have a Nightclub custom post type, with a number of ACF-powered fields, including events on each night. So far, the events are structured as Monday - Poster Image, Title. Tuesday - Poster Image, Title and so on.

What I'd like to do is only display events that are on tonight on the front page, but am having trouble narrowing the query down to ones with entries that are today. Here's the query I've done so far:

<div id="home-featuredevent">
        <h2>On Tonight</h2>
        <?php $date = date('l' ); ?>
        <?php $args = array( 'post_type'  => 'nightclub' , 'posts_per_page'  => 10, 'key'  => 'event_$date' , 'value'  => '' , 'compare'  => '!='   );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
        ?>
        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <a href="<?php the_field('event_$date' ); ?>"><?php the_field('event_$date_desc' ); ?></a>

At the moment, only the venue titles and links are showing. Now, I'm fairly sure I've got my variable calls wrong, and I'm concerned that the structure of the 'the_field('xyz') is going to make it a bit more complicated. I'm still starting out with PHP, so there is an excellent good chance I've just written bad code!

Any help would be great,

Tristan

share|improve this question

1 Answer 1

I'm not sure why you are comparing anything when you would normally just query a meta_key for a certain day, maybe I mis-understood the question.

The structure would be something like:

  meta_key => day // or $variable
  meta_value => Monday //for example

Then you just query posts for a particular day which has a meta field value that matches.

For example using pre_get_posts to alter your main query to only get posts where the value is equal to the actual day.

function wpse_78591_date( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {

        $today = date("l");

        $query->set('meta_key', 'day');
        $query->set('meta_value', $today );
    }
}
add_action( 'pre_get_posts', 'wpse_78591_date', 9999 );

or in your example it would be:

$args = array( 'post_type'  => 'nightclub' , 'posts_per_page'  => 10, 'key'  => 'day' , 'value'  => $today );

In my example I have hardcoded 'key' => 'day' but you can use a variable instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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