after creating variations the wp_term_relationships table is not getting updated with variation id / object_id and attribute term id / term_taxonomy_id
Because of this, I cannot perform tax_query on product_variation.
My tax_query example:
$query_args = array(
‘post_type’ => ‘product_variation’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => $attributes[‘limit’],
‘tax_query’ => array(
array(
‘taxonomy’ => ‘pa_purchase-as’,
‘field’ => ‘slug’,
‘terms’ => array(‘mirror’),
),
)
);
Note:
however, the attributes are getting saved perfectly for variations and I can get them via get_attributes(). It must be saving attributes as JSON in wp_post_meta.
Now, Woocommerce doesn't store the variations attributes in term_relationships table it stores the attribute value in meta key "attribute_your_att_slug".
In my case, the value is stored as meta key "attribute_pa_purchase-as".
So, if you want to get the variations based on attributes you can use meta query instead of tax_query to get results as I've used in the following example:
$query_args = array(
‘post_type’ => ‘product_variation’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => $attributes[‘limit’],
'meta_query' => array(
array(
'key' => 'attribute_'.trim($attributes['attribute']),
'value' => $terms,
'compare' => 'IN',
)
)
);
Hope that helps someone. :)
Related
I'm trying to figure out how to achieve a specific query to modify my search results for posts with WordPress. I'm trying to search via a custom field called "Common Authors".
There can be multiple Common authors, which is causing my query to sometimes fail. Here is what I've got for now:
<?php
...
$query->set('meta_key', 'common_authors');
$query->set('meta_value', serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('meta_compare', 'IN');
This is what I see when I var_dump the query:
'meta_key' => string 'common_authors'
'meta_value' => string 'a:1:{i:0;s:5:"17145";}'
This works fine if there is only one common_author.
However, there can be multiple common_authors for a post. Here is a meta_value example from the database:
a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}
Could somebody help me out, to figure out how to adapt my query, so that it would return this one as well?
Try This One Work Prefect!
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
if You Need To Use It In pre_get_posts
$meta_query = array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
);
$query->set('meta_query', $meta_query);
I have two post types:
Venues
Reviews
The Venues Post Type contains the following ACF custom fields:
Region
Sub Region
The Reviews Post Type contains one ACF custom field:
Venue (which is Post Object - Select Field)
I need to display all Reviews who's Venue is in a specific region and/or Sub Region.
Is this something that can be accomplished using WP_Query? Or do I need to do a fancy database call?
This is what I thought would initially work but it seems that you can not get the custom field of a post object as a meta_query..
$args = array(
'post_type' => 'review',
'posts_per_page' => 18,
'paged' => $paged,
'meta_key' => 'venue',
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
);
I think you need 2 loops here, first loop through the venues using region meta query (you could just use get_posts() or get_pages() instead of WP_Query too)
e.g.
'meta_query' => array(
array(
'key' => 'region',
'value' => 'napa-valley'
)
)
Then you can push the IDs of the venues in specific regions into an array
array_push($venue_ids, $post->ID);
Then you can use the $venue_ids array in your second loop which would loop through the review using a meta query to match the venues from your first loop ids to the post object ids selected in the review page.
'meta_query' => array(
array(
'key' => 'venue',
'value' => $venue_ids
)
)
Let me know if this is helpful and if you think this will work for you and I can offer further assistance if I haven't explained correctly or you need help.
I am trying to get specific product using meta key value. Any help would be highly appreciated.
I am trying to display a specific product when the product code is passed via a shortcode.
I have setup metabox and confirmed that the items have custom meta values with key "neproductinfo-ne_item_code"
$atts = shortcode_atts(
array(
'itemcode' => '',
),
$atts, 'products_catalog'
);
$woocommerce_loop['columns'] = 1;
$meta_query_args = array(
array(
'key' => 'neproductinfo-ne_item_code',
'value' => $atts['itemcode'],
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$products = new WP_Query( array (
'post_type' => 'product',
'post_status' => 'publish',
//'posts_per_page' => 1,
'meta_query' => $meta_query
));
I am getting list of all products regardless of any itemcode passed through shortcode
shortcode example
[products_catalog itemcode='1001']
No if condition found where you are checking the attribute value exists or not.
Based on existence of attribute value, you need to pass meta query. If there is no value then y you passs the meta_query to WP_Query ?
This question seems to be unanswered on the internet, perhaps because it is not possible. I simply want to query all posts where the repeater field 'has rows'. i.e.
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '',
'compare' => '=!'
)
);
I know an alternative is to run if statements inside the loop to filter them out, but that messes with some other logic which is based off of the number of posts in the query.
Does anyone have any thoughts on this?
Let's consider you have a Repeater Field labeled My Repeater Field and this repeater contains at least one field labeled A Field on the Repeater.
Assuming you have the default wp_ table prefix, you need to look at the DB's wp_postmeta table to notice that values for this field on the repeater are stored with the meta_key:
NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER
So, in our case, if a post has 3 rows on the repeater field, its values will be stored as:
my_repeater_field_0_a_field_on_the_repeater
my_repeater_field_1_a_field_on_the_repeater
my_repeater_field_2_a_field_on_the_repeater
Knowing this, if you want to query all posts having at least ONE row on the repeater, you could do:
$meta_query = [
[
'key' => 'my_repeater_field_0_a_field_on_the_repeater',
'compare' => 'EXISTS',
]
];
$args = [
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'post_type' => 'post',
'post_status' => 'publish',
];
$posts_array = get_posts( $args );
Note: As stated on WP Docs, you can only use the EXISTS comparison on WP >= 3.5 and you don't need to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up. I'm also assuming you are using PHP >= 5.4 so you can use short array syntax. If not, just replace [] for array().
You can query the wordpress database using the $wpdb object. ACF fields are saved in prod_postmeta on the database so that is where you will run your query. Your meta_value will be the key of your repeater field, so make sure you replace that in the query below. All keys for any ACF field with start out with field_ and then random characters/digits will follow like seen below. Then once you have the post id, you can run get_post() on those post ids. Let me know if you need anything else or have questions.
global $wpdb;
$results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'");
$echo $results;
This works. I have test it. Only by the "Welcome World" post it doesn't work.
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '0',
'compare' => '!='
)
));
$the_query = new WP_Query( $args );
The "repeater_field" is the name of the field, not the field_key. Here is the count of the rows.
My site has woocommerce plugin, so post_type is product. Each product has several custom fields used by another plugin (Compare Products Pro).
Now we would like to query products by URL so eg: www.domain.com/?post_type=product&custom_field=value
Is this possible? And how?
Any help would be highly appriciated!
You could add something like this to your query arguments:
'post_type' => $_GET['post_type'],
'meta_query' => array(
array(
'key' => '_woo_compare_field-',
'value' => $_GET['_woo_compare_field-'],
'compare' => '=',
)
),
But its very static, will not work for different meta key value pairs.
You could assign them to an array and append that array like this
$meta_queries = array(
'relation' => 'AND',
);
foreach($_GET as $key => $value) {
$result = array(
'key' => $key,
'value' => $value,
'compare' => '=',
);
$meta_queries[] = $result;
}
And append the $meta_queries array to the wordpress query arguments.
But this means all your get variables are used as meta key value pairs. You can ofcourse write some logic for this but it will never be pretty