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'),
)
);
Related
I'm working on a wordpress of a soccer club and I want to display events (matches) for the current week.
I have to sort by ACF field called "date_match" and not the date of the post itself but it doesn't work.
Matches are custom post type.
Here is my query
<?php
//define args
$args = array(
'post_type' => 'matchs',
'orderby' => 'meta_value',
'meta_key' => 'date_match',
'order' => 'ASC',
'posts_per_page' => 4,
// Using the date_query to filter posts from last week
'meta_query' => array(
array(
'key' => 'date_match',
'year' => date( 'Y' ),
'week' => date( 'W' )
)
)
);
//query
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
?>
…
<?php endwhile; ?>
<?php else: ?>
<!-- article -->
<article>
<h6>No match to display.</h6>
</article>
<!-- /article -->
<?php endif; ?>
And here is my configuration in ACF :
You can try something like:
$start = 'define your start date';
$end = 'define your end date';
// update your meta_query to search for posts between start and end
'meta_query' => array(
array(
'key' => 'date_match',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
<?php
//define args
//timestamp used to format the date
$thisMonday = strtotime('this week');
$thisFriday = strtotime('+6 days', $thisMonday);
$thisMonday = date('Ymd', $thisMonday);
$thisFriday = date('Ymd', $thisFriday);
$args = array(
'post_type' => 'matchs',
'orderby' => 'meta_value',
'meta_key' => 'date_match',
'order' => 'ASC',
'posts_per_page' => 4,
// Using the date_query to filter posts from this week
'meta_query' => array(
array(
'key' => 'date_match',
'value' => $thisMonday,
'compare' => '>='
),
array(
'key' => 'date_match',
'value' => $thisFriday,
'compare' => '<='
)
)
);
//query
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()): while ($the_query->have_posts()) : $the_query->the_post();
?>
There is the following code, the problem is that it displays all the goods in a row, regardless of status, but I would like to display only those that are "in stock".
function get_products($categories = array(), $product_type = 'featured_product', $paged = 1, $post_per_page = -1, $orderby = '', $order = '') {
global $woocommerce, $wp_query;
$args = array(
'post_type' => 'product',
'posts_per_page' => $post_per_page,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => $orderby,
'order' => $order
); }
I'm not really sure what you're trying to do with your code, as your function and your args seem to be unrelated... but if you're just trying to get the products, try a custom loop:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Do stuff.
endwhile;
endif;
wp_reset_postdata();
Note that I have not tested this and you clearly have something unique going on with how you're trying to display the posts, so you may need to make a few adjustments.
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 :)
I am trying to Query Woo-Commerce products based on the following
Featured
Popular
Promotional
Free
I was working to show them in isotop so I have to get all products and give specifiq tag based on my query to make filter. I was confused if I can make 4 Query as I have 4 different arguments / parameter.
I made 4 different query and got result in 4 different array. But there have problems.
I had done 4 Queries and Save result on 4 array. So there same products containing. I have to filter all products and if same product in more than one category I have to input tag like features free . as Isotop works.
If I unique marge array it will not show products in multiple category. But same products can be in two categories or can be in all categories so it should add tag in duplicate product and clear duplicate products list so that in result there will be unique product with multi tag.
Any idea how to do that ?
Below is my code which providing all products.
<?php
foreach ($filter_attr as $item) {
if ($item == 'featured') {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'orderby' => 'date',
'order' => 'desc',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$featured_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'popular'){
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$popular_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($popular_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'promotional'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => 'product',
'meta_key' => '_sale_price',
'meta_value' => '0',
'meta_compare' => '>='
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$promotional_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($promotional_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'freebies'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$freebies_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($freebies_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
}
}
?>
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();
?>