0

I wrote a short code to work with an event plugin (the plugin creates the custom post type of event). I want to pull all future events from the database that use a specific category and list them on the page in order of the event date. The events are showing up but the category selection is not working and it is also showing past events.

Here is my shortcode:

/* Exhibitions Shortcode */
add_shortcode( 'event_list', 'eventList' );
function eventList( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'event',
        'tax' => '',
        'post_status' => 'publish',
        'date_query' => array(
        array(
        'column' => 'event_start_date',
        'after' => date('F j, Y'),
        ),
        ),
        'posts_per_page' => '',
        'order' => 'ASC',
        'orderby' => 'event_start_date',
    ) );
    if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="eventListing">
    <div class="column oneThird">
           <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
            </div>
            <div class="column twoThird last">
           <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
           <?php echo get_the_content( $more_link_text, $stripteaser ); ?>
            </div>
            </div>
            <?php endwhile;
            wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}
/* END Exhibitons */

the shortcode in use is [event_list tax="exhibitions" posts_per_page="3"]

UPDATED CODE With Categories working:

/* Exhibitions Shortcode */
add_shortcode( 'event_list', 'eventList' );
function eventList( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'event',
        'tax_query' => array(
            array(
                'taxonomy' => 'event-categories',
                'field' => 'slug',
                'terms' => $atts['tax'],
            ),  
        ),
        'post_status' => 'publish',
        'date_query' => array(
            'after' => date('F j, Y'),
            'column' => 'event_start_date',
        ),
        'posts_per_page' => $atts['posts_per_page'],
        'order' => 'ASC',
        'orderby' => 'event_start_date',
    ) );
    if ( $query->have_posts() ) { ?>
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="eventListing">
    <div class="column oneThird">
           <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
            </div>
            <div class="column twoThird last">
           <a href="<?php the_permalink(); ?>"><h3><?php the_title(); ?></h3></a>
           <?php echo get_the_content( $more_link_text, $stripteaser ); ?>
            </div>
            </div>
            <?php endwhile;
            wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}
/* END Exhibitons */

2 Answers 2

0

Just by looking at your code I see a few places I'd investigate.

1) I don't see any use of the $atts that come through the shortcode. Specifically the 'tax' and 'post_per_page' attributes.

So try changing this;

'tax' => '',

to this:

'tax_query' => array(
    array(
        'taxonomy' => 'category',
        'field'    => 'slug',
        'terms'    => '$atts['tax']',
    ),
),

And same with the empty posts_per_page:

'posts_per_page' => '$atts['posts_per_page']',

Looks liek you got issue 1 fixed. Good job. :)

2) To fix the date issue we probably need to know how the custom field created by the events plugin is storing the event date.

EDIT: Yep. So it appears the plugin that created the custom post types also created a custom DB table. and if you are to get the event_start_date you'll need to refer to that custom table.

11
  • Sadly this did not fix the issue. The Time sort is "working" but isn't searching the event_start_date instead it is pulling from the publish date. and Category filter just isn't working at all... Feb 5, 2015 at 21:58
  • I also discovered that {tax} (string) was depreciated in wordpress so now I need to use something like this, array( 'post_type' => 'post', 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ), ); Feb 5, 2015 at 22:05
  • Ahh yes, ok. So how about the post_per_page? is that working? You only get 3 posts? ... I'll update my answer with proper taxonomy array.
    – Sean Grant
    Feb 5, 2015 at 22:09
  • Hey Sean I added the new code in the question above so it is what I have now. The limiter is work the only thing that I have left to fix is to only allow posts that are for future events to be shown. The database column is the event_start_date column. But no matter what I put there it only filters by publish date. Feb 5, 2015 at 22:13
  • Looks like you'll want to use meta_key or meta_query rather than the date_query. See here: codex.wordpress.org/Class_Reference/…
    – Sean Grant
    Feb 5, 2015 at 22:25
0

this will work with an theme but a couple things to note, the code is looking for a custom post type of 'event' and it is sorting the order of the events by 'event_start_date' if you were to change those to match whatever plugin you are using you could totally use it! you could also add those as attributes in the the shortcode itself but i am not a huge fan of super long shortcodes myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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