Event Espresso Query Modification? - wordpress

I am using Event Espresso with WordPress.
May u help me out in further query modification?
Hope you will :)
I want to use meta_query to list events on page.
Somewhat like below code.
$atts = array(
'title' => NULL,
'limit' => 10,
'css_class' => NULL,
'show_expired' => FALSE,
'month' => NULL,
'category_slug' => NULL,
'order_by' => 'start_date',
//'order_by' => 'end_date',
'sort' => 'DESC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => '2017-01-08 08:00:00',
'type' => 'DATETIME',
'compare' => '>=',
),
)
);
I want to implement search functionality for Event Espresso and i have those fields:
State - Dropdown (How to list all state? May be Venue)
Category - Dropdown
Start Date - Datepicker
End Date - Datepicker
Keyword - input
On submit those values will be submitted and based on those , i will get filtered events that are related with those values.
So how to implement this?
Please help.
Thanks in Advance

A slightly delayed response to this...
First, this depends on the version of EE you're using. They support both EE3 and EE4. I use EE4, so any reference to code I make is specific to version 4.
In order to create an events archive with a filtering functionality, you'd need to use the EE events archive. EE uses custom database tables and a lot of different post types to achieve what you see, so creating a simple archive with these filters won't work very well. Very little is stored in the _posts and _postmeta tables and you need to get post meta from related post types and tables that aren't laid out like WP_Query likes. They have a shortcode for the events list, which is laid out here and has a lot of the filters you're looking for, but no search functionality.
Their support forum has a lot of snippets and such created by their staff and there's a (long) related post about formatting the events archive page here. You'll also need to go through this documentation to see about their custom methods and hooks.
You could copy over and modify the archive templates they describe in a child theme to add search functionality to the top of the page. This would allow you to directly override the archive. You'll need to make use of some fancy filters, which are covered pretty clearly over on WPMU Dev.
The pieces of information you're looking for for filtering are here, minus keyword:
<?php
if( have_posts() ){
while ( have_posts() ){ the_post(); // enter the WordPress loop
$id = get_the_ID(); // get the ID of the current post in the loop, post ID = EVT_ID
$terms = get_the_terms( $id, 'espresso_event_categories' ); // get the event categories for the current post
if ( $terms && ! is_wp_error( $terms ) ) {
$cats = array();
foreach ( $terms as $term ) {
// do something
}
}
$event = EEM_Event::instance()->get_one_by_ID( $id ); // get the event OBJECT using EE's existing method
$venue = $event->venue(); // get the venue object for the current event
$state = $venue instanceof EE_Venue ? $venue->state_abbrev(); // get the event venue's state, but you can use state_name() to get the full name
// using the event to get the first and last date for the entire event. Sub $datetime for $event to do it per datetime
$start_date = $event->start_date('j M Y');
$end_date = $event->end_date('j M Y');
$start_time = $event->start_time(get_option('time_format'));
$end_time = $event->end_time(get_option('time_format'));
?>
<!-- Do some awesome layout stuff here -->
<?php
}
}
espresso_pagination();
?>
This will give you the meta for each post within the loop as variables, but you may want to pull them into pre_get_posts. You can just as easily create an array of $events and then filter it using the variables.
I'm not sure what your exact needs are regarding keyword. Do you mean tags? Title keywords? Description search? You'll need to narrow down exactly what you need this to do in order to write a function for it.

Related

ACF - filter relationship query to display WooCommerce product available in this category

I'm trying to create a custom category product sort order in WooCommerce using Advanced Custom Fields.
I have found a great guide to set it up. The approach is:
Add the ACF relationship field to the category page.
Config it to display only products.
Add a function that orders the category page based on the products you've added.
It works great!
The problem I have is that it always displays all the available products and not only the product available in this category.
I'm trying to use this function but the tag_id is not working.
add_filter('acf/fields/relationship/query', 'my_acf_fields_relationship_query', 10, 3);
function my_acf_fields_relationship_query( $args, $field, $post_id ) {
$args['tag_id'] = $category_id;
return $args;
}
Any suggestions?
You want to filter by product category terms, so you should not use:
[tag_id]
But instead use:
[term_id]
You follwed the tutorial, but the filter function is not working. You have created the custom field and inserted the function to create your custom product order. What is not working, if I got your question the right way, is the function that fetches an array of all the product IDs within a specific category. So you are getting all the available products and not the ones of the category you selected.
This should have been part of your question so others can find a solution:
This is the code from the tutorial you used: https://www.igoo.co.uk/2016/10/setting-a-custom-category-specific-product-sort-order-in-woocommerce-using-advanced-custom-fields/
function my_custom_product_order($q) {
if(!is_admin())
{
// fetch current category id from active query
$category_id = $q->get_queried_object_id();
// get array of all product IDs in current category
$product_ids = get_category_product_ids($category_id);
// get preferred order from ACF field
$product_ids_order_preferred = get_field('product_order', 'product_cat_' . $category_id);
// if we have some product sort order set…
if($product_ids_order_preferred)
{
// merge our preferred category ids array with the array of all products ids, and remove duplicates
$product_ids = array_unique(array_merge($product_ids_order_preferred, $product_ids));
}
// set the 'posts__in' argument to the new array of post IDs (unfortunately wordpress doesn’t let you just pass an array of IDs straight in here)
$q->set('post__in', $product_ids);
// set the query orderby value to observe the posts__in field
$q->set('orderby', 'post__in');
}
remove_action('woocommerce_product_query', 'custom_pre_get_posts_query');
}
add_action('woocommerce_product_query', ‘my_custom_product_order’);
In Line 7 of the code above, you are using the "get_category_product_ids($category_id)" function and give the $category_id as a parameter.
The "get_category_product_ids" function in your tutorial looks like this:
// helper function to fetch all product IDs from a specific category ID
function get_category_product_ids($category_id) {
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category_id,
'operator' => 'IN'
)
)
);
$ids = get_posts($args);
return $ids;
}
This function gets the parameter $category_id and uses it to make a post query and saving the resulting ids of the tax query.
This is the actual answer, which might help you:
If you want to find out why it is not working, you should first have a look at the $category_ id variable, because this value is the most important to make it work. So you can have a look if there is data inside the variable with var_dump inside your function after setting the variable:
var_dump($category_id)
Is the variable set correctly and do have data when the "my_custom_product_order" function is called? You should get the id of the current viewed category.
If you not get an id inside my_custom_product_order
you should have a look at the "get_queried_object_id()" function. It is possible to get this value with another way. The "get_queried_object()" function returns a WP_Term object. So you can also do:
$category_id = $q->get_queried_object();
$category_id = $category_id->term_id;
Maybe you now have the id you need.
If not, you should check the parameter $q, what is it for? Maybe this could cause the problem. You can try something like:
if (is_product_category()) {
$q = get_queried_object();
$term_id = $q->term_id;
}
If you are getting an id inside my_custom_product_order
then you should have a look at the "get_category_product_ids" function. Does the parameter work? Try var_dump inside this function and see if there is value handed over to the function. If not, you could get the variable another way. This seems redundantly and makes the parameters useless, but this way you can make sure that there is a category ID for your tax query. Put this at the start of your "my_category_product_ids":
$category_id = get_queried_object();
$category_id = $category_id->term_id;
Hope this will help you finding out why it's not working. Without knowing that it will be hard to fix the problem. If I'm right, the problem is with your $category_id not having a value for making the functions work.

How can i get post with multiple dynamic taxonomies

I am filtering custom post types using ajax. I am able to filter using single taxonomy but how to filter results with multiple taxonomies.
This is the query i tried:
Code:
$args=array('orderby'=>'date','post_status'=>'publish');
//sort by bank if isset
if(isset($_POST['bank']))
{
$args['tax_query']=array(
array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
)
);
}
if(isset($_POST['card_type']))
{
$args['tax_query']=array(
'relation'=>'AND',
array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
)
);
}
$query=new WP_Query($args);
But it only shows result with filtering from one taxonomy not both.
I think you are working on right track but a small mistake in argument may cause result to be rendered wrong.
Instead of 'relation'=>'AND', use 'relation'=>'OR'
'relation'=>'AND' will fetch result when both taxonomy condition match, while 'relation'=>'OR' condition will compare with any of the taxonomy.
click here to view the WP Query arguments
Actually yout array formation is wrong for tax_query.
Please check updated code below.
If you don't use $args['tax_query']['relation']= 'AND', then also it will work for you.
Please have a try with below code snippet.Update you code with below snippet.
$args = array(
'orderby'=>'date',
'post_status'=>'publish'
);
//sort by bank if isset
if(isset($_POST['bank'])){
$args['tax_query'][]= array(
'taxonomy'=>'banks',
'field'=>'id',
'terms'=>$_POST['bank']
);
}
if(isset($_POST['card_type'])){
$args['tax_query']['relation']= 'AND';//you can remove this
$args['tax_query'][]= array(
'taxonomy'=>'cardtype',
'field'=>'id',
'terms'=>$_POST['card_type']
);
}
$the_query = new WP_Query( $args );

WooCommerce - include custom fields in the search function

So I'm looking to include the functionality of displaying content by its custom fields as well as its title and content.
I need to be able to search for orders, as well as subscriptions, on WooCommerce by custom field as well as the normal method. Is there any way I can, without adding additional search forms or booleans, simply get Wordpress to display posts that match with the search term by their custom fields, too?
I have used the following code thanks to a responder on here:
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"gender",
"birthdate"
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => '=='
));
}
$query->set("meta_query", $meta_query);
};
}
add_filter( "pre_get_posts", "custom_search_query");
This works great when searching orders, but what I need to search is subscriptions, where it doesn't work.
Help would be much appreciated!

WooCommerce Queries

Good Day,
I am fairly new to WordPress and WooCommerce.
But I am fairly versed in PHP and MySQL.
So my question is, how do I go about making a specific query for WooCommerce where I can point it to specific meta. For example, if I wanted to make a query to call all the different categories within WooCommerce>
I would normally call it like this;
$args = array(
'number' => 'null',
'orderby' => 'name',
'order' => 'ASC',
'columns' => '4',
'hide_empty' => '1',
'parent' => '',
'ids' => ''
)
Ok so that would be my query, now what I am so struggling with is what do I do with it it now? Cause that part there is what everyone shows, no one tells you or shows you how to actually use the query or where to start.
I have used the basic WP query function, but then the only categories I receive back come from WP and WC.
So how do I point it directly to WC?
Furthermore, these hooks annoy me so. So how do I go about doing it the normal way, bypassing WC and WP functions and build my own query;
$query = "SELECT * FROM wc_categories";
$result = mysql_query($query);
$temp_array = mysqli_fetch_array($result);
That is how I would love to do it, cause no one explains hooks and how to use it, or edit it, everyone assumes that you are pro-leet when it come to familial of WP workings.
So if anyone can point me in the right direction, I would really appreciate it.
And I would love you long time if you could explain to me how to go about my 2nd code block cause that is my preferred way.
Kind Regards,
DK
Creating query using standard wp/wc function is easy. For example if you want to get customer details for specific orders. Required codes might look like this (notice the global function):
global $post, $woocommerce, $the_order;
if ( empty( $the_order ) || $the_order->id != $post->ID )
$the_order = new WC_Order( $post->ID );
$user_info = get_userdata( $the_order->user_id );
$biling_email = $the_order->billing_email;
$phone_no = $the_order->billing_phone;
If you want to query a custom order meta field, you may use a code something like below. You need to know the custom field name in database:
$custom_field = get_post_meta( $post->ID, 'custom_field', true );
For custom query you need to initialize global wpdb function first by adding global $wpdb function before your query. Even this will work with a WP page only, it'll not work with a page created outside WP. Below is an example:
global $wpdb
$table = $wpdb->prefix . 'postmeta'; //Good practice
$orderno = $wpdb->get_results( "SELECT * FROM $table");

how do i enter a var in a wordpress custom query?

how do I put a custom var in the following line of the below
AND $wpdb->postmeta.meta_value = 'email'
I want to put in a var for 'email'
something like
AND $wpdb->postmeta.meta_value = $var
anyone know how to do this is Wordpress. I think i need to bind?
re: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
It depends a bit on context, but if you really are performing a custom query (as, say, part of a widget) you would provide it as an argument to WP_Query:
$query = new WP_Query( array( 'meta_value' => 'user#example.com' ) );
Oftentimes this would include a corresponding meta_key in that argument array.
If you want to modify the currently running query (say, on a category page, only show posts that match a certain criteria) you would perform that modification during the pre_get_posts action. Recommended reading: Andrew Nacin's You Don't Know Query.
I had done that using custom query plugin Go here
else custom your query like this
<?php
$args = array('post_type' => 'page','meta_query' => array(array('key' => 'email','value' => 'yes','compare' => '%')));
$var = new WP_Query($args);
// The Loop
while ( $var->have_posts() ) : $var->the_post();
$P_ID = get_the_ID();
endwhile;
// Reset Post Data
wp_reset_postdata();
Hope it helps

Resources