Group by wordpress posts by meta value - wordpress

I have about 2000 woocommerce products on my wordpress database, each product has an ACF select field named place which value is local or regional.
My goal is to group by each products by the place meta field so i I thought that following code do the job :
class Search
{
public function __construct()
{
add_action("pre_get_posts", [$this, "filterProducts"]);
add_filter('posts_groupby', [$this, 'groupByFilter'] );
}
public function filterProducts($query){
// ... code
// This where i alter que query
// .. code
return $query;
}
public function groupByFilter($groupby)
{
global $wpdb;
return $wpdb->postmeta . ".meta_key = 'place'";
}
}
But, this not working, and output only one product while the expected result is :
-- LOCAL
product
product
-- REGIONAL
product
product
I know foreach over each product is a solution, but for performance reasons i am looking do to it directly throught main query.
NOTE : if it helps, I use timber and in the search.php file I have this piece of code :
$context = Timber::get_context();
$context["posts"] = new \Timber\PostQuery();
// print the results
dump($context['posts']);
UPDATE :
The sql results output this query :
SELECT 6288gjvs_posts.* FROM 6288gjvs_posts
WHERE 1=1
AND 6288gjvs_posts.post_type = 'product'
AND (6288gjvs_posts.post_status = 'publish'
OR 6288gjvs_posts.post_status = 'acf-disabled'
OR 6288gjvs_posts.post_status = 'complete'
OR 6288gjvs_posts.post_status = 'paid'
OR 6288gjvs_posts.post_status = 'confirmed'
OR 6288gjvs_posts.post_status = 'unpaid'
OR 6288gjvs_posts.post_status = 'pending-confirmation'
OR 6288gjvs_posts.post_status = 'cancelled'
OR 6288gjvs_posts.post_status = 'private')
GROUP BY 6288gjvs_postmeta.meta_key = 'place'
ORDER BY 6288gjvs_posts.menu_order, RAND(1348526234)
Copied it and pasted on phpmyadmin with error output :
#1054 - Champ '6288gjvs_postmeta.meta_key' not known in group statement
So i left join the post meta :
SELECT 6288gjvs_posts.post_title, 6288gjvs_posts.ID, 6288gjvs_postmeta.post_id
FROM 6288gjvs_posts
LEFT JOIN 6288gjvs_postmeta
ON 6288gjvs_posts.ID = 6288gjvs_postmeta.post_id
AND 6288gjvs_posts.post_type = 'product'
GROUP BY 6288gjvs_postmeta.meta_key = 'place'
Without success
UPDATE 2
Hello, so finally i opted with two queries, i didn't found any solution, now I have to manage two paginations in one page :(. If someone find a solution, i always follow up this post and may be useful for others.
Thank you for your helps

If you want to filter your posts by a meta key, you should definitally take a look at WP_Meta_Query.
So like you did with 'tax_query' in the 'pre_get_posts' filter, you should add another query in the main query like this:
$meta_query[] = array(
'key' => 'place',
'value' => 'your_value',
'compare' => '=',
);
$query->set('meta_query', $meta_query);
I hope you find this usefull and it solves your problem.

It is a lot easier and advisable to get posts using WP_Query instead of modifying the SQL codes that may affect other post types and queries. In your scenario where you are getting WooCommerce products, use WC_Product_Query. Below is an example that will get you products with meta_key = place
function get_xx_products()
{
$args = array(
'my_custom_var' => array(
array(
'key' => 'place',
'value' => '',
'meta_compare' => 'EXISTS'
)
),
'status' => 'publish'
);
return wc_get_products($args);
}
function hook_my_custom_var($query, $query_vars)
{
if (!empty($query_vars['my_custom_var'])) {
$query['meta_query'] = $query_vars['my_custom_var'];
}
return $query;
}
add_filter('woocommerce_product_data_store_cpt_get_products_query', 'hook_my_custom_var', 10, 2);
You can read more about it here - https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query

Related

Get product variation ID from variation SKU [duplicate]

I'm working on a separate templates page, which page gets woocommece product sku using custom field of wordpress post. i need to get product id of that sku for create woocommece object and do further things, here is my code.
global $woocommerce;
//this return sku (custom field on a wordpress post)
$sku=get_field( "product_sku" );
if($sku!=''){
//need to create object, but using sku cannot create a object,
$product = new WC_Product($sku);
echo $product->get_price_html();
}
is there way to get product id before create object, then i can pass the product id to WC_Product class constructor and create object.thank you
WooCommerce 2.3 finally adds support for this in core.
If you are using this version, you can call
wc_get_product_id_by_sku( $sku )
You can use this function (found here). Google is your friend!
function get_product_by_sku( $sku ) {
global $wpdb;
$product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
if ( $product_id ) return new WC_Product( $product_id );
return null;
}
WooCommerce product is a special post type. Because of this WP_Query might be used to find product by SKU and other parameters. This might be used as an alternative when you need to restrict your search by some other criterias.
For example you might want to specify language if you use Polylang (or other plugins) for site translations. Or you can restrict search by product type.
They do direct SQL query in the WooCommerce method get_product_id_by_sku which I think is perfectly fine in many cases. But might not work if you use translations, it will return random product but not the one in current language.
Example code to find product by SKU using WP_Query (with restriction by product type and language):
public function find( string $lang, string $sku ) {
$query = [
'lang' => $lang,
'post_type' => 'product',
'meta_query' => [
[
'key' => '_sku',
'value' => $sku,
'compare' => '='
]
],
'tax_query' => [
[
'taxonomy' => 'product_type',
'terms' => [ 'grouped' ],
'field' => 'name',
]
]
];
$posts = ( new WP_Query() )->query( $query );
return count( $posts ) > 0 ? $posts[0] : null;
}

Programmatically construct a dynamic dropdown field in Contact Form 7

I looked around here and by search engine, but unfortunately I couldn't find a solution for myself.
So, I now ask assistance with a function that I need to customize for the Contact Form 7 WordPress plugin. The function was from another question.
In a drop-down menu (select) I need two details (workshop name and date) in one option field. Both details come from the same post of a custom post type. The first detail is a post_title, the second is a custom-field from Meta-Box plugin.
The following function works in principle, but it only returns the one or the other detail. Probably the solution is within the foreach construct. But I don't know how it works.
I would be very grateful for support!
[UPDATE 2018-08-12]
After further research, I've found the solution at this post and changed the function accordingly.
The solution should look like this:
<select>
<option value="workshop name – date">workshop name – date</option>
...
</select>
This is the function:
add_filter( 'wpcf7_form_tag', 'dynamic_field_choose_workshop', 10, 2);
function dynamic_field_choose_workshop ( $tag, $unused ) {
if ( $tag['name'] != 'workshop' )
return $tag;
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$ID = $custom_post->ID;
$tag['values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['raw_values'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
$tag['labels'][] = $custom_post->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $ID);
}
return $tag;
}
There is CF7 extension that will do this for you. Checkout the Smart Grid-Layout for CF7, it introduces a new tag called dynamic_dropdown. This is is what you want to use. The dynamic_dropdown creates a select field and allows you to populate the field options using either a taxonomy, titles of a post type, or a filter. You want to use the filter option to actually construct the options as per your requirement. The tag popup window is self explanatory, however if you get stuck post a comment below and I'll give you some more tips.
Using the following dynamic_dropdown tag,
[dynamic_select workshop-date-select class:select2 "source:filter"]
it creates a <select name="workshop-date-select"> dropdown field which will be converted into a select2 jquery field on the front end, and its values dynamically created using the following function placed in the functions.php file,
add_filter('cf7sg_dynamic_dropdown_custom_options', 'filter_options',10,3);
function filter_options($options, $field_name, $form_key){
/*first we verify if this is the right field from the right form
in case multiple forms with similar fieldd exiss.
the $form_key is a unique key exposed by the Smart Grid-layout plugin
instead of using form IDs to make forms and code more portable across servers.*/
if($form_key != 'my-form' && $field_name != 'workshop-date-select') return $options;
$options = array();
//load your options programmatically, as $value=>$name pairs.
$args = array (
'post_type' => 'workshop',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'numberposts' => - 1,
);
$workshops = get_posts( $args );
foreach($workshops as $workshop){
$val = $workshop->post_title . ' - ' . rwmb_get_value('workshop_meta_boxes_date', '', $workshop->ID);
$options[$val]=$val;
}
return $options;
}
this will create the desired dropdown select field in the front end.
NOTE of CAUTION: I would populate the option values as the workshop post ID rather than the same text as the option label. When the form is submitted the value of the post ID can be used to populate the notification email with the desired workshop title and date. This gives more flexibility to expand the reported information in the future.

NinjaForm - How To Search & Retrieve By DateTime?

I'm using NinjaForm plugin on wordpress. Here how to search and retrieve data:
<?php
$args = array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => array(
'34' => 'checked',
'54' => 'Hello World',
),
);
// This will return an array of sub objects.
$subs = Ninja_Forms()->subs()->get( $args );
// This is a basic example of how to interact with the returned objects.
// See other documentation for all the methods and properties of the submission object.
foreach ( $subs as $sub ) {
$form_id = $sub->form_id;
$user_id = $sub->user_id;
// Returns an array of [field_id] => [user_value] pairs
$all_fields = $sub->get_all_fields();
// Echoes out the submitted value for a field
echo $sub->get_field( 34 );
}
What I want to do is searching by DateTime fields. How do I do that?
I have tried change args like this but result same.
$args = array(
'form_id' => 5,
'date_modified'=> '2015-07-25 3:19:09'
);
or like this
$args = array(
'form_id' => 5,
'date_modified'=> '< 2015-07-25 3:19:09'
);
Did I do wrong?
Find Ninja DB Table:
Go into your database using phpmyadmin or something and find the table Ninja Forms is using. Hopefully they're using their own table. If not, you can search each wp table for some of the arg data that you know returns a form from Ninja_Forms(). Or go into the Ninja plugin code and try and find where they interact with the db to find which table they write into.
Write your own mysql search code:
Instead of using Ninja's class to search, use wordpress's built in mysql search and throw in the table you found in step 1.
GLOBAL $wpdb;
$wpdb->get_results($wpdb->prepare("SELECT * FROM `ninja_table` WHERE `date_modified` = %s", $strDate));
I haven't tested, but this would be my course of action.
Use begin_date and end_data parameters to get the submissions
$args = array(
'form_id' => $form_id,
'begin_date' => '2015-07-20 0:00:00',
'end_date' => '2015-07-25 3:19:09'
);
$subs = Ninja_Forms()->subs()->get( $args );

How to Query Posts if Post Meta Value is multidimensional array?

I was using the following meta Query, but I'm still getting irrelevant posts.
$current_query['meta_query'] = array(
array(
'key' => '_game_selected_platforms',
'value' => $platform->term_id,
'compare' => 'LIKE'
),
);
Whereas the meta value was multidimensional array as follows:
get_post_meta('$postID','_game_selected_platforms',false);
So here is what i did. i used the pre_get_posts filter, to further dig into that array and search if the value exist, then i used post__in
function latest_articles_of_platform($query){
$platformobject = get_query_var('get_latest_articles');
global $wpdb;
$getallposts = $wpdb->get_results( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_type = 'post'", OBJECT );
if ($getallposts){
$postsin = array();
foreach ($getallposts as $getallpost) {
$platformsofposts = get_post_meta($getallpost->ID,'_game_selected_platforms',false);
if(in_array($platformsofposts->term_id, $platformsofposts ) ){
$postsin[] = $getallpost->ID;
}
}
} // if $getallposts
if (get_query_var('get_latest_articles')) {
// var_dump($getallposts);
$query->set('post__in',$postsin);
}
}
add_action( 'pre_get_posts', 'latest_articles_of_platform' );
I was searching for help with existing topic but i did not get any solution.
IMPORTANT
$current_query['get_latest_articles'] = $platform;
I have also added this line in query to identify that it is my custom query but this is only possible if you have access to front end query otherwise there are other methods to identify if it that query which you want to filter.

Wordpress - multiple WP Query objects into one?

In Wordpress it's possible to create own WP Querys for the loop. An example is this:
$my_query = new WP_Query(array('post_parent' => 3, 'post_type' => 'page'));
Another example is this:
$my_query = new WP_Query(array('cat' => 1, 'post_type' => 'post'));
I want a loop that presents pages AND posts from the same loop.
Now to my question. Is it possible to combine these two objects into one? If it is, how? I'm NOT interested in creating two different loops.
In case you don't want to do it with SQL, this is how I did my search page.
Basic problem: When doing a meta_query, wordpress thinks I want the condition to be joined with "AND" instead of "OR".
So Wordpress looks for a page with title/content = "myContent" AND the aioseop_keyword "myContent". This (in my case) lead to zero results, despite there was a page with matching SEO keyword.
To get around this, I make two queries. Sounds simple, BUT: the Loop didn't want to recognize the posts, despite there are posts in the $post object. I found this solution after taking a look on the have_posts() function : it refers to other variables than just the $post object.
$term = get_search_query(); // same as $_GET['s']
# the normal search:
$wordpress_keyword_search =& new WP_Query(array(
's' => $term,
'showposts' => -1
));
# now push already found post IDs to an array, so we can exclude them from the meta search.
foreach ($wordpress_keyword_search->posts as $post_)
$exclusion[] = $post_->ID;
# now do the meta query search
$aioseop_keyword_search =& new WP_Query(array(
'post__not_in' => $exclusion,
'post_type' => 'any',
'showposts' => -1,
'meta_query' => array(
array(
'key' => '_aioseop_keywords',
'value' => $term,
'compare' => 'LIKE',
)
)
));
# merge the two array posts.
# post_count and found_posts must be added together also.
# otherwise have_posts() returns false.
# see: http://core.trac.wordpress.org/browser/tags/3.6.1/wp-includes/query.php#L2886
$wordpress_keyword_search->posts = array_merge($wordpress_keyword_search->posts, $aioseop_keyword_search->posts );
$wordpress_keyword_search->found_posts = $wordpress_keyword_search->found_posts + $aioseop_keyword_search->found_posts;
$wordpress_keyword_search->post_count = $wordpress_keyword_search->post_count + $aioseop_keyword_search->post_count;
Then use this in a simple loop:
if ($wordpress_keyword_search->have_posts()) {
while($wordpress_keyword_search->have_posts()) {
$wordpress_keyword_search->the_post();
# now you simply can:
the_title();
the_content();
}
} else {
echo '<p>Sorry, no posts found</p>';
}
what you want would translate to a WHERE ... OR ... condition or a UNION in SQL, eg.
SELECT * FROM posts WHERE (post_parent = 3 AND post_type = 'page')
OR (cat = 1 AND post_type = 'post')
or
SELECT * FROM posts WHERE post_parent = 3 AND post_type = 'page'
UNION
SELECT * FROM posts WHERE cat = 1 AND post_type = 'post'
from looking at the source and the way WP constructs SQL from WP_Query(), i don't think this is possible: there is no OR'ing nor UNION of query vars.
the only thing that comes to my mind is writing a plugin that implements the posts_where filter (applied to the WHERE clause of the query that returns the post array). you could call this plugin with your different WP Querys, and the plugin would get their WHERE parts and could OR them together.
see also http://codex.wordpress.org/Custom_Queries

Resources