How to simplify my WP_Query(s)? - wordpress

Can anybody help me to simplify the WP_Query(s) below please? They have the very same structure, but one to target the future, and one for past events.
<?php
$future = date('Ymd', strtotime("+1 year"));
$now = date('Ymd', strtotime("now"));
$past = date('Ymd', strtotime("-1 year"));
?>
<?php
$args_future = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array($now, $future),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query_future = new WP_Query($args_future);
?>
<?php
echo '<h2>Future events</h2><ul class="future-events">';
while($query_future->have_posts()) {
$query_future->the_post();
echo '<li>'.get_the_title().'</li>';
} echo '</ul>'; wp_reset_postdata();
?>
<?php
$args_past = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array($past, $now),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
$query_past = new WP_Query($args_past);
?>
<?php
echo '<h2>Past events</h2><ul class="past-events">';
while($query_past->have_posts()) {
$query_past->the_post();
echo '<li>'.get_the_title().'</li>';
} echo '</ul>'; wp_reset_postdata();
?>

Your code isn't too bad. Splitting it into several loops is pretty much unavoidable in this case if you want to maintain readability. All I did here was organize your code into a multidimensional array. This is untested, but should get you started:
<?php
$future = date('Ymd', strtotime("+1 year"));
$now = date('Ymd', strtotime("now"));
$past = date('Ymd', strtotime("-1 year"));
$args = array(
'future'=>array(
'ul_class'=>'future-events',
'label'=>'Future Events',
'query_args'=>array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array($now, $future),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
)
),
'past'=>array(
'ul_class'=>'past-events',
'label'=>'Past Events',
'query_args'=>array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_date',
'value' => array($past, $now),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
)
)
);
foreach($args as $arg){
$q = new WP_Query($arg['query_args']);
printf('<h2>%s</h2><ul class="%s">', $arg['label'], $arg['ul_class']);
while($q->have_posts()) : $q->the_post();
echo '<li>'.get_the_title().'</li>';
endwhile;
echo '</ul>';
}
wp_reset_postdata();
?>

Related

Shortcode with multiple meta_key attributes

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 class="tribe-mini-calendar-event event-0 first last">';
$output .= '<h4 class="tribe-events-title">' . get_the_title() . '</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');
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,
);

How to hide text if query has no posts?

I want to hide the text with the word reviews if query has no posts. Please ask for suggestions.
is image > enter image description here
<h1 class="section-title"><span>Reviews</span></h1>
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>
No need of jQuery, just put the h1 tag after the If statement and before while. Then if your query is not empty it will display the title otherwise not
Like this:
<?php
$terms = get_the_terms( $post->ID , 'movies', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'post',
'cat' => '21',
'tax_query' => array(
array(
'taxonomy' => 'movies',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'IN' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 6,
'orderby' => 'date',
'post__not_in'=>array($post->ID)
) );
//Loop through posts and display...
if($second_query->have_posts()) { ?>
<h1 class="section-title"><span>Reviews</span></h1>
<?php while ($second_query->have_posts() ) : $second_query->the_post();
get_template_part( 'template-parts/content', 'list-01' );
?>
<?php endwhile; wp_reset_query(); }?>

WP Bakery woocommerce show product subcategory (FREE)

I wanted to share this work with you since I did not find any solution on the internet, so I had to build mine.
The problem was that the client wanted to show the subcategories within the main category using a shortcode. Only the name of the subcategories, without thumbnail or number of products.
So create a file called "products-subcategory.php" in plugins/bezel-addons/vc/shortcodes/.
And I incorporated it in plugins/bezel-addons/vc/shortcodes.php
In my case my template is the Bezzel https://themeforest.net/item/bezel-creative-multipurpose-wordpress-theme/20014332
But I think you can implement it in anyone using Visual Composer or WP Bakery.
products-subcategory.php.
<?php
/* Product subcategory */
vc_map(
array(
'name' => 'Product subcategory',
'base' => 'bezel_products_subcategory',
'icon' => 'ti-align-left',
'description' => 'Product subcategories',
'category' => __( 'Bezel', 'bezel-addons'),
'params' => array(
array(
'type' => 'dropdown',
'param_name' => 'orderby',
'heading' => 'Order BY',
'value' => array(
'Name' => 'name',
'ID' => 'term_id'
),
),
array(
'type' => 'dropdown',
'param_name' => 'order',
'heading' => 'Order',
'value' => array(
'Upward' => 'ASC',
'Falling' => 'DESC'
),
),
array(
'type' => 'dropdown',
'param_name' => 'empty',
'heading' => 'Show empty subcategories',
'value' => array(
'Yes' => 0,
'No' => 1
),
)
)
)
);
add_shortcode( 'bezel_products_subcategory', 'bezel_products_subcategory' );
function bezel_products_subcategory( $atts ) {
global $wp_query;
extract( shortcode_atts( array(
'taxonomy' => 'product_cat',
'orderby' => 'name',
'order' => 'ASC',
'empty' => 0,
'hierarchical' => 1
), $atts ) );
$cat = get_queried_object();
$category_id = ($cat->parent) ? $cat->parent : $cat->term_id;
$args2 = array('taxonomy' => $taxonomy,'parent' => $category_id,'hierarchical' => $hierarchical, 'orderby' => $orderby, 'order' => $order,'hide_empty' => $empty);
$categories = get_categories( $args2 );
$categories_cnt = count(get_categories( $args2 ));
$selcat[$cat->term_id] = 'current-cat';
if ($categories_cnt != 0){
$sub_cats = get_categories( $args2 );
if($sub_cats) {
$output = '<div class="vc_wp_categories wpb_content_element">';
$output .= '<div class="widget widget_categories">';
$output .= '<ul>';
foreach($sub_cats as $sub_category) {
$output .= '<li class="cat-item cat-item-'.$sub_category->term_id.' '.$selcat[$sub_category->term_id].'">'.$sub_category->cat_name.'</li>';
}
$output .= '</ul>';
$output .= '</div>';
$output .= '</div>';
}
}
return $output;
}
?>

How do i show only 1 post from latest 10 post?

my english language is not good. sorry!
this code is for checkbox in advanced custom field plugin.
i want show only 1 post(randomly) from latest 10 post.
please help me. thanks
('posts_per_page' => 10) and ('numberposts' => 10) is not working.
<?php
$gallery = array(
"offset" => "0",
'showposts' => '1',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
// query
$qgallery = new WP_Query( $gallery );
?>
<?php if( $qgallery->have_posts() ): ?>
<?php while( $qgallery->have_posts() ) : $qgallery->the_post(); ?>
<div class="fromgallery">
<a href="0" class="frgall">
<span class="frgdesc"><?php the_title() ?></span>
</a></div>
<?php endwhile; ?><?php endif; ?>
Try this
$gallery = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
$qgallery = new \WP_Query( $gallery );
I only do change in your code. For randomly one post you need to use '_shuffle_and_pick' => 1 , 'posts_per_page' => 10 is from 10 post and 'order' => 'DESC' is for latest posts. for custom '_shuffle_and_pick' you need to add
add_filter( 'the_posts', function( $posts, \WP_Query $qgallery )
{
if( $pick = $qgallery->get( '_shuffle_and_pick' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
To show Random post , Please use below script:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
//check is post found
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
I hope it will help you :)

Wordpress meta_value_num not working

I'm trying to simply order posts by a numerical value set in a custom field. I've scoured online resources and still cannot determine why this isn't working. Any ideas?
<?php
$args = array(
'post_type' => 'calendar',
'meta_key' => 'event_date_new',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php echo the_field('event_date_new'); ?>
<?php endwhile; ?>
have you tried with
'orderby' => 'meta_value_num meta_value' or 'orderby' => 'meta_value meta_value_num'?
This Query works
$wp_query_args['post_type'] = 'event';
$wp_query_args['paged'] = $paged;
$wp_query_args['posts_per_page'] = 10;
$wp_query_args['orderby'] = 'meta_value_num';
$wp_query_args['order'] = 'DESC';
$wp_query_args['meta_key'] = 'event_end_date';
$wp_query_args['meta_query']= array(
array(
'key' => 'event_end_date',
'compare' => '<',
'value' => date('Ymd'),
)
);
This Query doesn't work.
$wp_query_args['post_type'] = 'event';
$wp_query_args['paged'] = $paged;
$wp_query_args['posts_per_page'] = 10;
$wp_query_args['orderby'] = 'meta_value_num';
$wp_query_args['order'] = 'ASC';
$wp_query_args['meta_key'] = 'event_start_date';
$wp_query_args['meta_query']= array(
'relation' => 'OR',
array(
'key' => 'event_start_date',
'compare' => '>=',
'value' => date('Ymd'),
),
array(
'key' => 'event_end_date',
'compare' => '>=',
'value' => date('Ymd'),
)
);

Resources