Created
July 30, 2019 21:05
-
-
Save jbd91/3d2f56a1a053807f3976a1dbb6b35690 to your computer and use it in GitHub Desktop.
WP Query Shortcode -- Custom Post type using Custom Taxonomy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // create shortcode with parameters so that the user can define post queries | |
| add_shortcode( 'recurring-events', 'recurring_events_parameters_shortcode' ); | |
| function recurring_events_parameters_shortcode( $atts ) { | |
| ob_start(); | |
| // define attributes and their defaults | |
| $a = shortcode_atts( array ( | |
| 'type' => 'recurring_event', | |
| 'posts' => -1, | |
| 'categories' => '', | |
| ), $atts ); | |
| // Check if category value is a single value or comma separated list. If the list is comma separated, create an array of values to be used. | |
| $terms = $a['categories']; | |
| if (!is_array($terms)) { | |
| $terms = explode(',', $a['categories']); | |
| } | |
| // define query parameters based on attributes | |
| $args = array( | |
| 'post_type' => $a['type'], | |
| 'posts_per_page' => $a['$posts'], | |
| 'tax_query' => [ | |
| [ | |
| 'taxonomy' => 'recurring_event_categories', | |
| 'field' => 'slug', | |
| 'terms' => $terms, | |
| ], | |
| ], | |
| ); | |
| $query = new WP_Query( $args ); | |
| if ( $query->have_posts() ) { ?> | |
| <div class="recurring-events"> | |
| <?php | |
| print_r($terms); | |
| ?> | |
| <div class="fusion-row"> | |
| <div class="recurring-events--listing"> | |
| <?php while ( $query->have_posts() ) : $query->the_post(); ?> | |
| <div class="recurring-events--event"> | |
| <h2><?php the_title(); ?></h2> | |
| </div> | |
| <?php endwhile; | |
| wp_reset_postdata(); | |
| ?> | |
| </div> | |
| </div> | |
| </div> | |
| <?php | |
| return ob_get_clean(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment