woocommerce, product link by reviews loop - php

I created the very simple loop here, the aim is to extract only reviews that have a value equal to or greater than 4 stars. And so far so good. Now from this loop I would also like to get the link to the specific product. Anyone have any idea how to get this information?
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
/* stuff */
}

Working on it a little longer, I gave myself the answer, I attach the solution to my problem:
<?php
$reviews = get_comments( array(
'status' => 'approve',
'post_status' => 'publish',
'post_type' => 'product',
'meta_query' => array( array(
'key' => 'rating',
'value' => array('4','5'),
) ),
) );
foreach( $reviews as $review) {
$postId = $review->comment_post_ID;
the_permalink($postId);
}
?>

Related

Woocommerce: Create short-code with parameters

I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.

Visual composer custom query - excluding meta_key

I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as yes.
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
Any help would be appreciated.
Thanks
There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here, but it's not working either.
//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();
$args_exclude = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
),
),
);
$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
foreach ($exclude_posts->posts as $post)
{
$exclude_id[] = $post->ID;
}
}
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);
// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
print_r($post);
}
Hope this helps!
See: visual composer wordpress query for post grid
Try this:
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
echo http_build_query($args);
// Result:
post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS
http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e
I would recommend to avoid use array('post') in case if only one post type, so just use post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS
P.S.
possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));

Count posts with meta value

I'm trying to show the number of posts that have certain meta key values for the current user.
This is my code:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
'key' => 'color',
'value' => array('red', 'blue')
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
echo $the_count;
Thi is counting ALL posts for the current user, ignoring the meta key values.
I only need the $the_count to be the number of posts that have a meta key value 'red' or 'blue' for the current user.
What am I doing wrong?
Thanks in advance!
I am not sure, but you could try something like this:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_key' => 'color',
'meta_value' => array('red', 'blue')
);
$posts_query = new WP_Query($args);
$the_count = $posts_query->post_count;
echo $the_count;
If you want to use the meta_query array, you have to put the meta_key and meta_value in a subarray:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'author' => $current_user_id,
'meta_query' => array(
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
),
);
$posts_array = get_posts( $args );
$the_count = count($posts_array);
This is because you can use multiple meta_key to combine them.
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'color',
'value' => array('red', 'blue'),
),
array(
'key' => 'size',
'value' => array('l', 'xl', 'xxl'),
),
),

merge/blend 2 queries if and conditional statement is true

I have this wp query...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
But I want to add this to the query if my $user_admin condition is true...
if ($user_admin)
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
)
);
So I run this it seems to break my loop, but not cause a fatal error...
$downloads = new WP_Query(array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
));
if ($user_admin) {
$downloads = new WP_Query(array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
OK my question is essentially this... How do I blend the two $downloads variables if the $user_admin condition equals true.
But the fastest and correct method of actually going about doing this as my method does not work.
From looking at your code it looks like you could merge the array together and then create the new WP_Query object. From your description I understand that you are saying that you want the queries to be blended into one and not the results of the query blended into one.
$args = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$args = array_merge($args, array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
));
}
$downloads = new WP_Query($args);
I also was wondering you indicated that your current code seems to break your loop. Exactly what is happening with your first code example. Are you getting a blank page or simply not having any articles returned?
Another thing to note when checking if a user is an administrator you can also use is_admin() instead of $user_admin.
Function Reference/is admin
Try:
$query = array(
'post_type' => 'download',
'paged' => $paged,
'posts_per_page' => 20
);
if ($user_admin) {
$query2 = array(
'meta_query' => array(
array(
'key' => 'download_access_rules',
'value' => 'genpo',
'compare' => 'NOT IN'
)
)
);
$query = array_merge($query, $query2);
}
$downloads = new WP_Query($query);
Hope this helps!

Retrieve CPT Posts from a specific category | WordPress

I am currently retrieving posts from a custom post type using the following array:
$args = array(
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);
It works great.
Now, I was wondering if I could retrieve these same posts but ONLY if they appear in the category ID '6'.
Can someone explain how I update my current code to allow this?
Thank you.
Use 'cat' instead of 'category':
$args = array(
'post_type' => 'event',
'cat' => 6,
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);

Categories