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();
}
}
?>
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();
?>
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 have two custom fields for views. weekly_views and all_views. The weekly views custom field is deleted every week and starts counting views again from 0. So now what I want to achieve is show 12 posts by weekly views but when the custom field is deleted and unless there are views on those posts the query shows nothing. I want to show here posts by all_views instead of no posts.
My query goes as follows but it's not working as I want. In short what I want to achieve is to show posts by weekly_views custom field but if there's no post then show posts by all_views. And also if there's less than 12 posts by weekly_views then show weekly_views posts first and then remaining posts by all_views.
$args = array(
'post_type' => array( 'custom_post_type_1', 'custom_post_type_2'),
'posts_per_page' => '12',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'weekly_views',
),
array(
'key' => 'all_views',
),
),
);
The above code is returning me posts but are sorted by all_views.
Edit
The new query that's working for me
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ($the_query->post_count < 12) {
$countweeklyposts = $the_query->post_count;
$showallpostscount = 12 - $countweeklyposts;
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => $showallpostscount,
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query2 = new WP_Query( $args2 );
}
?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
<?php while ($the_query2 -> have_posts()) : $the_query2 -> the_post(); ?>
//Code to show posts goes here
<?php
endwhile;
wp_reset_postdata();
?>
You could do this too if you want a little less code
<?php
$args = array(
'post_type'=> array( 'custom_post_type1', 'custom_post_type2'),
'posts_per_page' => '12',
'meta_key' => 'weekly_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$args2 = array(
'post_type'=> array( 'band', 'artist'),
'posts_per_page' => '12',
'meta_key' => 'all_views',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
if ($query->post_count > 12) {
$query_args = $args;
}else if($query->post_count < 12){
$query_args = $args2;
}
$query = new WP_Query( $query_args );
while ($query -> have_posts()) : $query -> the_post();
//Code to show posts goes here
endwhile;
wp_reset_postdata();
?>
I view the other post but I found nothing, I'm on since 3 days :
I want display 3 'evenements' in the order ASC
but 2018 is always before 2017
$auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
),
)
)
);
?>
someone has an idea ?
I have updated your code .Please try your updated query.
<?php $auj = date('Ymd');
$queryEvent = new WP_Query(
array(
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
),
)
));
?>
I have replace 'orderby' value & added 'type' parameter in meta_query.
Hope, this may be helpful to you.
As your date format is not in Y-m-d so you need to define your own
logic for shorting
function wh_posts_orderby($orderby, $query) {
//Only for custom orderby key
if ($query->get('orderby') != 'yyyymmdd_date_format')
return $orderby;
if (!( $order = $query->get('order') ))
$order = 'ASC';
global $wpdb;
$fieldName = $wpdb->postmeta . '.meta_value';
return "STR_TO_DATE(" . $fieldName . ", '%Y%m%d') " . $order;
}
add_filter('posts_orderby', 'wh_posts_orderby', 10, 2);
Add the ^^above code to your active theme functions.php file.
Now you can sort your post by date (format YYYYMMDD)
$auj = date('Ymd');
$args = [
'category_name' => 'evenements',
'posts_per_page' => 3,
'meta_key' => 'date_de_fin',
'orberby' => 'yyyymmdd_date_format', //check this line
'order' => 'ASC',
'meta_query' => [
'relation' => 'AND', //added this
[
'key' => 'date_de_fin',
'value' => $auj,
'compare' => '>=',
'type' => 'DATE'
],
]
];
$queryEvent = new WP_Query($args);
if ($queryEvent->have_posts()) :
/* Start the Loop */
while ($queryEvent->have_posts()) : $queryEvent->the_post();
//you post
endwhile;
endif;
Related answer How do I query posts and use the 'orderby' attribute to order posts in loop according to date 'meta_value'?
Hope this helps!
I resolve by problem with a SQL query :
$queryEvent = $wpdb->get_results( 'SELECT * FROM cci_posts INNER JOIN cci_postmeta ON cci_posts.id = cci_postmeta.post_id INNER JOIN cci_term_relationships ON cci_posts.id = cci_term_relationships.object_id WHERE cci_term_relationships.term_taxonomy_id = 4 AND cci_postmeta.meta_key = "date_de_fin" AND cci_postmeta.meta_value > 20170221 AND cci_posts.post_status = "publish" ORDER BY cci_postmeta.meta_value ASC LIMIT 3', OBJECT );
//$queryEvent = new WP_Query($args);
?>
<?php if ( $queryEvent ) : ?>
<?php /* Start the Loop */ ?>
<?php foreach ($queryEvent as $post) {
setup_postdata($post); ?>
I have done this query and is working.I have a lot of child posts and i plan to display only child posts when listing the archive page of my custom post type city-guide.
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
.....
}
I have tried
$all = get_posts(array('post_type'=> 'city-guide', 'posts_per_page' => -1));
$parents = array();
foreach ($all as $single)
{
$kids = get_children($single->ID);
if(isset($kids) && !empty($kids) && count($kids) >= 1)
{
$parents[] = $single->ID;
}
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'post__not_in' => $parents,
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
....
}
This did not work.Please help me find out where i went wrong.
I know it's an old question but hoping I can help someone that finds their way here looking for the same thing I was.
You can show ONLY child posts by excluding any posts with post_parent = 0 using the 'post_parent__not_in' argument:
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged,
'post_parent__not_in' => array(0)
);
This avoids the need to loop thru each parent post to get each child.
I see you are trying to push the IDs into an array but why not just use the IDs while you are looping through them while getting the children within the loop at the same time? The example below is how I would tackle this.
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
$i=1; while( $query->have_posts() ): $query->the_post();
$parentID = get_the_ID();
$childrenArgs = array(
'post_type' => 'page',
'post_parent' => $parentID ,
);
$children = get_children($childrenArgs);
foreach ($children as $child){
echo '<h1>' . $child -> post_title . '</h1>';
$content = $child -> post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
endwhile;
?>
I think you need to look into the action pre_get_posts. Something like this in your functions.php would do the trick.
function namespace_custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( $query->query["post_type"] == 'custom_post_type' ) {
$query->set( 'post_parent__not_in', 0 );
}
}
return $query;
}
add_action( 'pre_get_posts', 'namespace_custom_query_vars' );
There's a decent post about this here. Though note that the code on this page does not compile for small syntax errors.
What about using relations? A simple disjunctive union should do the charm.
$args = array(
'post_type' => POST_TYPE,
'posts_per_page' => 36,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => true
),
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
Or is there a reason why not to consider this?