I added two custom fields: day (event_day) & month (event_month) (both of type radio) for CPT Events. Now i want to be able to get posts by meta_key day and month.
The shortcode works except the part with $meta_query.
Here is how shorеcode should look like : [tribe_custom_events_list_mm_wed cat="Rodrigo" num="6" day="Monday" month="October"]
Bellow is the code responsible for the shortcode, added in functions.php
function tribe_custom_events_shortcode($atts, $content = null)
{
global $post;
extract(shortcode_atts(array(
'cat' => '',
'num' => '',
'order' => 'ASC',
'orderby' => 'post_date',
'taxonomy' => 'tribe_events_cat',
'field' => 'name',
'day' => '',
'month' => '',
), $atts));
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
$day = $day;
$month = $month;
$meta_query = array(
array(
'key' => 'event_day',
'value' => '$day',
'compare' => '='
),
array(
'key' => 'event_month',
'value' => '$month',
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => array($tax_query),
'meta_query' => array($meta_query),
);
$output = '';
$posts = get_posts($args);
foreach ($posts as $post) {
setup_postdata($post);
$output .= '<div >';
$output .= '<h4 ><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h4>';
$output .= '</div>';
}
echo '<pre>' , var_dump($meta_query) , '</pre>';
wp_reset_postdata();
return '<div>' . $output . '</br>' . '</div>';
}
add_shortcode('tribe_custom_events_list_mm_wed', 'tribe_custom_events_shortcode');
CodePudding user response:
This should work for you. There were a few errors in your code... Noted in the comments below.
$tax_query = array(
'taxonomy' => $taxonomy,
'field' => $field,
'terms' => $cat,
);
/* This is unnecessary since $day already = $day
$day = $day;
$month = $month;
*/
$meta_query = array(
array(
'key' => 'event_day',
'value' => $day, // Don't put quotes around variables
'compare' => '='
),
array(
'key' => 'event_month',
'value' => $month,
'compare' => '='
),
);
$args = array(
'post_type' => 'tribe_events',
'posts_per_page' => $num,
'order' => $order,
'orderby' => $orderby,
'tax_query' => $tax_query, // This is already an array defined above
'meta_query' => $meta_query,
);