WooCommerce Queries - wordpress

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");

Related

Is there any function in Wordpress for set title

I want to know is there any function for the set title in WordPress. Can anyone help me?
As far as I know, there is not easy function for this. You can use the $wpdb object to accomplish this and update the "blogname" option. Definitely use the helper functions and avoid running queries on the database directly. The update helper function forces you to specify "WHERE" clauses, which helps prevent you from completely screwing up your DB.
global $wpdb;
$wpdb->update(
"{$wpdb->prefix}options",
array(
'option_value' => 'New blog title'
),
array(
'option_name' => 'blogname'
),
array(
'%s'
),
array(
'%s'
)
);
I would read up on $wpdb before messing with it, to make sure you are running safe operation. And as always, use a test environment first: https://developer.wordpress.org/reference/classes/wpdb/

WordPress is_search() function always false

I want to add some custom conditions and do some other things on a search WordPress results page. So, I’m checking the value of is_search() to make sure I’m only applying the condition at the right time. So in my child theme's functions.php I put:
if (is_search()) {
...
}
But this always returns false, even with a url like http://mysite/?s=something and my theme's search.php template is being used! Is this not valid to call from functions.php, or am I misunderstanding this function's purpose?
For that matter, looking at a template hierarchy like presented at https://wphierarchy.com, how does WP even know it’s a search results page? How does it know to proceed down the "search results" path? I’ve spent some time perusing the source code but haven’t been able to find the right spot yet.
I tried calling is_search() from search.php, and it works! From some more source code perusal, I discovered that you can't call is_search() from functions.php because functions.php is called too early in the process. Apparently functions.php is considered part of the "WordPress library" and is loaded in wp-blog-header.php line 13, while the template is called in line 19.
In between the two is the wp() function, which sets up the query. That query is actually set up in user.php line lines 1198-1212:
$args = array(
'post_type' => $this->post_type,
'post_name__in' => array( $this->request_type ),
'posts_per_page' => $posts_per_page,
'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
'post_status' => 'any',
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
);
...
$requests_query = new WP_Query( $args );
To answer my second question (how does WordPress know it's a search), it’s as simple as having the "s=" query string parameter. Not sure what to do if you want a search result that does not have a search string (say a custom search based solely on pulldowns or something). If you need that, I would recommend looking thru the source file WP-query.php and seeing exactly what you need to pass. Don’t be scared of looking at the source code; It’s very educational!

Adding data to wp_sitemeta table

Is there a way to add a row to the wp_sitemeta table in Wordpress Multisite? I'm thinking just like for wp_usermeta, there are functions such as add_user_meta( $user_ID, $meta_key, $meta_value), add_user_meta( $user_ID, $meta_key, $meta_value) and similar, there might be a way to do the same for wp_sitemeta?
I have searched, but can't seem to find information about adding to this table anywhere. Can it be done?
WordPress provides the function update_metadata($meta_type, $object_id, $meta_key, $meta_value, [$prev_value]) (see the codex) that can (and probably should) be used for this. It will update the key value if it exists, and add it if it does not exist.
To update wp_sitemeta, set $meta_type = 'site'. The $object_id should be the network ID, which is almost certainly "1". (WordPress only supports a single network with ID "1" via its UI, although the WP DB has schema support for multiple networks.) The $meta_key and $meta_value parameters are for the metadata key and value, respectively.
Note too that wp_sitemeta is for network-wide options, not for blog-specific (child site) options.
If you're able to work at the shell command prompt and have access to the WP-CLI command line client, you can also use the wp network meta update <id> <key> [<value>] [--format=<format>] command. (I have not tested whether this adds or updates as needed, but I expect it does.)
It depends on what you are trying to add. You can use $wpdb
<?php
global $wpdb;
$main_blog_prefix = $wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
$meta_table = $main_blog_prefix."sitemeta";
$insertedRow = $wpdb->insert( $wpdb->prepare(
$meta_table ,
array(
'site_id' => BLOG_ID_CURRENT_SITE,
'meta_key' => 'some_meta',
'meta_value' => "asd asd asda sda sda sd"
)
)
);
?>

Event Espresso Query Modification?

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.

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