I need to include children comments (replies) to comments which I'm filtering with a meta query.
If I'm filtering comments that have a rating 3/5, I need to include their children with the query (even though the children don't match the meta query).
$comments = get_comments( array (
//'meta_key' => 'rating',
'order' => 'ASC',
'orderby' => 'date',
'post_id' => $_POST['post_id'],
'status' => 'approve',
'meta_query' => array(
array(
'key' => 'rating',
'value' => $_POST['rating']
)
)
) );
Is there a way to "force include" children that don't match initial query?
(To see the issue live, try to filter the reviews by 3 stars on this page, and notice how the review reply is not included in the filter: https://herbalnitro.com/product/extreme-energy/)
The issue that you get the only comment with specific meta but children comments do not inherit this meta obviously. So you need to do it in two steps: 1) get comments with meta. 2) get children comments for a parent with which is with meta.
// get comments with meta
$comments = get_comments( array (
'order' => 'ASC',
'orderby' => 'date',
'post_id' => $_POST['post_id'],
'status' => 'approve',
'meta_query' => array(
array(
'key' => 'rating',
'value' => $_POST['rating']
)
)
) );
// find children comments
$comments_children = array();
foreach ( $comments as $comment ) {
$comments_children += get_comments(array('parent' => $comment->comment_ID, 'status' => 'approve', 'hierarchical' => true));
}
// combine all comments
$comments = array_merge($comments, $comments_children);
// print comments template if needed
wp_list_comments(array(), $comments);
Related
I have a custom post type "product", and two custom taxonomies: "productfamily" and "industry".
On my single product page, I need to show products that are in the same productfamily and industry.
Some products might be in a few different industries...and my related products section only needs to match one of the industries of the current post to show up.
Here's what I have so far...
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'productfamily',
'field' => 'slug',
'terms' => $prodfam,
'operator' => 'IN'
),
array(
'taxonomy' => 'industry',
'field' => 'term_id',
'terms' => $prodindustry,
'operator' => 'IN'
),
),
'orderby' => 'title',
'order' => 'ASC',
)
);
If I change the "AND" relation to "OR", it seems to partly work by showing products from the same "productfamily", but doesn't seem to be taking the "industry" into account at all.
How can I get this to show related products based off of my two custom taxonomies please? Thanks in advance for your help.
As you described in comment that you have slugs array in variables but in one of your tax query condition, you've used term_id to match with slug. that is incorrect.
You can directly use the terms ids instead of slugs since it's dynamic things. in function wp_get_post_terms you can pass array( 'fields' => 'ids' ) as 3rd param and it will give you array of ids. so you don't have make an extra loop.
Then you'll have to check both terms array if they both are empty of one of them or both of them as values?
then you can check them individually and then add the tax query part if ids are available.
This is how you can write the code in a clean way with proper checks:
global $post;
// Get the terms ids array,
// we can pass 'fields' => 'ids' in 3rd param so we don't need to run the loop to collect ids.
$product_family_ids = wp_get_post_terms( $post->ID, 'productfamily', array( 'fields' => 'ids' ) );
$industry_ids = wp_get_post_terms( $post->ID, 'industry', array( 'fields' => 'ids' ) );
// Prepare query args.
$query_args = array(
'posts_per_page' => '4',
'post_type' => 'product',
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
),
);
// We need to check if both ids are not empty.
// if both empty we don't wanna run query.
if ( ! empty( $product_family_ids ) || ! empty( $industry_ids ) ) {
// If product family terms are available.
if ( ! empty( $product_family_ids ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'productfamily',
'field' => 'term_id',
'terms' => (array) $product_family_ids,
'operator' => 'IN',
);
}
// If industry terms are available.
if ( ! empty( $industry_ids ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'industry',
'field' => 'term_id',
'terms' => (array) $industry_ids,
'operator' => 'IN',
);
}
$related_posts = new WP_Query( $query_args );
if ( $related_posts->have_posts() ) {
while ( $related_posts->have_posts() ) {
$related_posts->the_post();
/**
* DO you thing here.
*/
}
}
wp_reset_postdata();
}
Note: I have not tested the code so there might be syntax errors, if you use the code and find errors after using the code please let me know so that I could fix errors in my answers. Also, Make sure you have site backup and FTP access to fix the errors, Don't add code from WordPress backend
I have been through several topics on sorting taxonomies and custom fields, including some that were promising. However, I am still not constructing my query args correctly it seems.
I am "fixing" someone else's code in a custom theme that is used in a multisite configuration, but I am able to override/filter the query on a per-site basis.
The query is called via the theme with the following:
return apply_filters( 'theme_query_args_filter', $args);
I have custom fields in the "role" taxonomy that include:
last_name
first_name
Pretty common I think.
However, when the args are executed, the following filter args are sorting only by last name (key part is the else clause):
function my_args_filter( $args ) {
if (is_home() && !is_search()) {
$args['meta_key'] = 'event_date';
$args['orderby'] = 'event_date';
$args['order'] = 'DESC';
}
else {
$tax = $args['taxonomy'];
$theterm = $args['term'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
}
add_filter( 'theme_query_args_filter', 'my_args_filter' );
I've tried to modify the orderby as indicated in https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ to use an array to do a multisort, but I'm hitting my head up against the wall. I think the problem is that the code is written using a mixture of old ways of doing things and new ways.
Any advice is appreciated. According to the example on in the docs above, I SHOULD be able to pass in multiple meta key/value/orders via an array, but I'm just not getting it.
Thanks for any leads you might have. (long-time listener, first-time caller)
(I also looked at https://wordpress.stackexchange.com/questions/109849/order-by-desc-asc-in-custom-wp-query but I couldn't extrapolate that example to this one either)
Figured it out on my most recent try. Hat tip to this solution https://wordpress.stackexchange.com/questions/249881/multiple-custom-fields-for-orderby-in-wp-query by Nath. if there is a more elegant way to do it, please let me know.
$tax = $args['taxonomy'];
$theterm = $args['term'];
$paged = $args['paged'];
$args = array (
'taxonomy' => $tax,
'term' => $theterm,
'paged' => $paged,
'posts_per_page' => '10',
'meta_query' => array(
array(
'relation' => 'AND',
'last_name' =>
array(
'key' => 'last_name',
'compare' => 'EXISTS',
),
'first_name' =>
array(
'key' => 'first_name',
'compare' => 'EXISTS',
),
),
),
'orderby' => array (
'last_name' => 'ASC',
'first_name' => 'ASC',
)
);
}
I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.
I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.
I have a json file with data for custom taxonomy.
And then I need to filter that.. I do not have any problem with postmeta link and other fields. But i can't get the category into json. And then can't filtering that.
I know the answer must be very simple but i can't figure it out for 2 days.
function getItems( $meta_query ) {
$params = array(
'post_type' => 'property',
'nopaging' => true,
'post_status' => 'publish',
'meta_query' => array(),
$meta_query = array('meta_query'=>array(), 'relation' => 'AND');
if (!empty($_POST['city']))
{
$params['meta_query'][] =array(
'key' => 'property_city',
'value' => $_POST['city'],
'compare' => 'LIKE'
);
}
if(!empty($_POST['parish']))
{
$params['meta_query'][] = array(
'key' => 'property_state',
'value' => $_POST['parish'],
'compare' => 'LIKE'
);
}
//..................ETC..........................
What i have for category now:
//if (!empty($_POST['category']))
/*{
$params['meta_query'][] = array(
'taxonomy' => 'pcategory',
'field' => 'slug',
'terms' => $_POST['category']->slug
);
}*/
$itemsQuery = new WP_Query();
$properties = $itemsQuery -> query($params);
// add property details
foreach ($properties as $key => $property) {
// options
$property->optionsDir = get_post_meta($property->ID, 'property', true);
$city = get_post_meta($property->ID, 'property_city', true);
$parish = get_post_meta($property->ID, 'property_state', true);
$link = get_permalink($property->ID);
$category = get_the_terms($property->ID,'pcategory' );
$properties[$key]->link = array(
'link' => $link,
);
//postmeta
$properties[$key]->postmeta = array(
'city' => $city,
'parish' => $parish,
'region' => $region,
'link' => $link,
'category' => $category,
);
}
return $properties;
}
$category = get_the_terms($property->ID,'pcategory' );
Above code will return, all details like, term id, name, slug etc. information about that term, as an array object.
You need to extract the required details.
Also, "pcategory" is custom taxonomy.
Therefore, in below code,
$properties[$key]->postmeta = array(
'city' => $city,
'parish' => $parish,
'region' => $region,
'link' => $link,
'category' => $category,
);
'category' will not be interpreted properly, as it is custom taxonomy and not default wordpress taxonomy.
If these parameters are sent to WP_Query(), you can mention 'tax_query' as below,
'tax_query' => array(
array(
'taxonomy' => 'pcategory',
'field' => 'slug',
'terms' => 'demo'
)
Here, "demo" is example slug for custom taxonomy.
Even, single post can have multiple category associated with it. Therefore, you need to extract desired category details.
In 'tax_query' you can also specify, "term_id" or other details.