merge/blend 2 queries if and conditional statement is true - php

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!

Related

woocommerce, product link by reviews loop

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);
}
?>

Count WP_Query returns 0 in Wordpress

Trying to implement a custom pagination in Wordpress and I cannot get the number of posts to dynamically create the pagination element.
$loop = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'orderby' => 'post_date',
'order' => 'date',
'posts_per_page' => $per_page,
'offset' => $start,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
$count = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
return $count->post_count;
The first query with the $loop returns the posts that I need. But when I return the $count or $count->post_count, it returns 0.
You need to reset the first query. So add this code after first loop and query
wp_reset_postdata();
wp_reset_query();
Then let me know the result.
Thanks
Posts per page is set to -1.
You could just count Loop?

Ordering Wordpress posts by multiple meta values

Using get_posts(), I need to first retrieve posts that fall on a certain day (the day is set by a custom field - just the date, not time). I do this by using a meta key/value. Then, I need to order these posts based on the time of day (which is a separate custom field, just time, not date). So essentially I need to pull in all the events that fall on a given day, and order them according to the time.
First I grab the day, using a custom field:
if ( get_field('festival_day') ) {
$day_stamp = get_field('festival_day');
}
Then I set my arguments for the query:
$args = array(
'posts_per_page' => -1,
'post_type' => 'event',
'meta_key' => 'event_date',
'meta_value' => $day_stamp
);
$events = get_posts( $args );
So.. the question is, how do I query the other custom field (which is the start time), and then sort by that time? The time field key is event_start_time.
Thanks!
You can user WP_Query to retrieve your events and you can query it something like below:
$args = array(
'post_type' => 'event',
'order' => 'DESC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_time',
'meta_query' => array(
array(
'key' => 'event_start_time',
'value' => 'yourValue_here',
'compare' => '>='
),
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => '='
)
)
);
$query = new WP_Query( $args );
UNTESTED but it should work.
Check this code, it should work to sort your post according to time from resulting post of day.
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'event_start_time',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'event_date',
'value' => $day_stamp,
'compare' => 'IN'
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>'. get_the_title().'</li>';
}
echo '</ul>';
} else {
// no posts found
}

meta_query not working in wp_query

My post type is product. I use a checkbox field with meta key is ht_featured, meta value when I print_r is array([0] => featured).
My WP_Query:
$the_query = new WP_Query(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
);
It doesn't show any post.
I tried with value => 'featured' and 'compare' => 'EXISTS' but it not working.
WP_query needs to be passed in an array. use following code and let me know if any prob.
$the_query = new WP_Query (array (
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
You can refer to the discussion at wordpress forum:
http://wordpress.org/support/topic/how-to-wp_query-meta_query-value-string-contain-in-key-string
You're passing all of this into WP_Query as individual arguments when they should be contained in an array.
$the_query = new WP_Query( array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN',
),
),
) );
Can you clarify your point about the checkbox? I'd suggest simply updating 'ht_featured' with either 'yes' or 'no' when you save the product. Then change your 'value' in the meta query to 'yes' and remove the 'compare'.
Are you sure there is no php error?
I think WP_Query needs to be passed in an Array
$the_query = new WP_Query(
array(
'post_type' => 'product',
'showposts' => 12,
'meta_query' => array(
array(
'key' => 'ht_featured',
'value' => array('featured'),
'compare' => 'IN'
)
)
));
I had a similar problem until I used the function get_posts() rather than creating a new WP_Query. See if that helps...

WordPress Query: ORDER BY CASE WHEN

I want to first display the posts that match a particular meta_key => cr_id and order those by another 'meta_key' => 'cr_list_price' and then display the rests of the posts that are also ordered by 'meta_key' => 'cr_list_price'.
This is my current code which does everything I want except show the posts where cr_id = 4.
$args = array(
'post_type' => 'properties',
'paged' => get_query_var( 'paged' ),
'meta_query' => array(
array(
'key' => 'cr_list_price',
'value' => array($minPrice, $maxPrice),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'cr_prop_bed',
'value' => $beds,
'compare' => '>='
),
),
'orderby' => 'meta_value_num',
'meta_key' => 'cr_list_price'
);
$loop = new WP_Query( $args );
If I was using raw MySQL I'd do something like ORDER BY CASE WHEN in my query, however I'm not sure how or if I can accomplish this with WordPress.
I wonder if you could add a filter to the query by adding that little extension on there. I'm thinking maybe posts_orderby
Function
function filter_case($orderby = '') {
$orderby .= 'CASE WHEN [some conditions]';
return $orderby;
}
Before Query
add_filter( 'posts_orderby', 'filter_case' );
$wp_query = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_case' );
I cant gaurantee this will work first time but it could be worked on if you think it's worth pursuing? List of WP_Query filters are here.

Categories