How to made function to count total user registered on Wordpress site with just specific roles (eg: manager) and just users that registered current month only?
Example code, it will output user registered all time, I want it just ouput user registered on current month only.
function totalusertoday() {
global $wpdb;
$query = "SELECT COUNT(ID) FROM {$wpdb->prefix}users WHERE user_status = 0";
return $wpdb->get_var($query);
}
echo 'Total User Today: ' . totalusertoday();
Have a look at the Database Model:
http://codex.wordpress.org/images/9/97/WP3.8-ERD.png
To count all new users from this month, simply select all Users with registred Timestamp > then 1. day of this month.
To get only users with a specific role you have to select the user and the wp_usermeta, in wp_usermeta is a row called wp_capabilities.
http://wordpress.org/support/topic/name-of-database-fields-for-user-rolespermissions
Basically it's just a bit of sql query.
Solved! I'm just put filter for current month and year on user registered. And I've made simple code for this. Here is example code:
<?php
global $wpdb;
$date = getdate();
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users WHERE MONTH(user_registered) = $date[mon] AND YEAR(user_registered) = $date[year]" );
echo "<p>User count is {$user_count}</p>";
?>
Related
I am using the following code to solve this but this is not working for orders from guest customers . However this is working fine for the orders belonging to some registered user/customer but not for the orders belonging to guest customers.
Solution credit to LoicTheAztec for answer
function cristmas_bulk_editing_orders(){
if(!is_admin()) return; // Will work only from Admin Backed.
else {
$order_id = 9458;
$new_customer_id = 479;
// Getting the postmeta customer ID for 'order' post-type
$customer_id = get_post_meta( $order_id, '_customer_user', true );
var_dump($customer_id);
// If it's an existing order and doesn't have already this user ID
// It update the customer ID
if( !empty($customer_id) && $new_customer_id != $customer_id )
update_post_meta($order_id, '_customer_user', $new_customer_id,0);
echo 'order updated';
}
}
cristmas_bulk_editing_orders();
ORIGINAL ISSUE
We imported the orders via woocommerce order export & import plugin from woocommerce team ..
But in the process something went wrong.. Most of the orders were not assigned any customer ..
So now when ever a new customer registers he/she is assigned 1 of these orders automatically ..
So basicallly all of them see 1 order in their recent orders which belongs to some other guest cusotmer , then they have all the information about other customer . their email etc..
So one option is I find out all the orders(with issues that is no customer assisned to them ) and I assign them to admin ..but this also have some issuses.....
SO is there any other option that these new registered users don't get old orders assigned..
Please help
May be you could try this other similar function based on SQL queries, that should do the trick. Before starting, make a database backup. I have add controls (or limitations) like :
A max date (in YYYY-MM-DD 00:00:00 date time format)
Order number range
So here is that code:
function easter_bulk_editing_orders(){
if( ! is_admin() ) return; // Will work only from Admin Backed.
else {
$processed_orders = array();
// Define the replacement user ID
$replacement_user_id = '2500';
// Define the DATE LIMIT for guest orders max date limit
$max_date = '2017-12-09 00:00:00';
// Define an order range
$order_id_min = '0';
$order_id_max = '100000';
global $wpdb;
// Get all guest orders below a defined max date
$old_guest_orders = $wpdb->get_results( "
SELECT pm.*, p.post_date
FROM {$wpdb->prefix}postmeta AS pm
LEFT JOIN {$wpdb->prefix}posts AS p ON pm.post_id = p.ID
WHERE pm.post_id BETWEEN $order_id_min AND $order_id_max
AND pm.meta_key LIKE '_customer_user'
AND ( pm.meta_value LIKE '0' OR pm.meta_value LIKE '' )
AND p.post_date <= '$max_date'
" );
foreach( $old_guest_orders as $guest_order ){
$meta_id = $guest_order->meta_id;
$wpdb->query( "
UPDATE {$wpdb->prefix}postmeta as pm
SET pm.meta_value = '$replacement_user_id'
WHERE pm.meta_id = '$meta_id'
" );
// Set each order ID in an array
$processed_orders[] = $guest_order->post_id;
}
// Testing ( raw output of processed orders IDS )
var_dump($processed_orders);
}
}
// Run the function
easter_bulk_editing_orders();
Code goes in function.php file of your active child theme (active theme or in any plugin file).
You have to use this function only once and to remove it afterwards (see below).
USAGE:
Once this code is pasted and saved on function.php file, display or reload any Admin page from backend within your browser.
Now you can comment the function this way and save:
// easter_bulk_editing_orders();
Check that the orders have been changed as you want, and remove all this code.
Code is tested and works.
There is error in your importing so try to re-import. Have you tried WP All Import Premium. It will import anything with drag and drop titles, categories, and meta fields. you can also assign new meta name to post meta while importing. If you need more help then share more details.
I have a custom table my_users.
When a person signs up for membership to my site through s2member, I want to get the WordPress wp_user id and insert a new row into my_users.
Where do I do this?
Thanks in advance.
This will help you to save data in your custom table if you save table with wp prefix you can include that with table name.
<?php global $wpdb;
// for get user id
$user_id = get_current_user_id();
$arrayinsert = array('user_id'=>$user_id,'Type_User'=>get_current_user_role()); ?>
$insert_result = $wpdb->insert('my_users',$arrayinsert);
if ($insert_result) {
$varId = $wpdb->insert_id;
} else {
echo "Error:". $wpdb->last_query;
}
?>
I wanna display total number of comments that other users submitted for all posts of an author! for example something like this :
get_author_posts_total_number_of_comments($author->ID);
any idea ?
function get_author_posts_total_number_of_comments($authorID)
{
global $wpdb;
$sql = "SELECT SUM(comment_count) AS total FROM $wpdb->posts WHERE post_author=%d";
return $wpdb->get_var($wpdb->prepare($sql,$authorID));
}
I want to have my own chat. (I can't use the Chat module because I have to personalize it.) I have to retrieve all the users who are online, but I can't see any variable for that.
I am only able to get the name of the currently logged-in user, but not the rest of the logged-in users.
You can fetch a list of all logged in users by querying the sessions table. I'm assuming you're using Drupal 6.
<?php
$result = db_query('SELECT uid FROM {sessions} WHERE uid != 0');
$users = array();
while($user = db_fetch_array($result)) {
$users[] = user_load($user);
}
The query excludes sessions for uid = 0 as these are anonymous users. $users is the array of user objects as described in the Drupal API Docs.
You can optimize this if you already know what part of the user objects you will use (e.g. just the user id and name) by removing the user_load() in while loop and adding to the query a join with the users table, as each user_load() makes one additional query. The following would get you a list of logged in users' id and names:
<?php
$result = db_query('SELECT u.uid, u.name FROM {sessions} s INNER JOIN {users} u ON u.uid = s.uid WHERE s.uid != 0');
$users = array();
while($users[] = db_fetch_array($result));
Since logged in users never time out (you can stay logged in indefinitely), it may be useful to exclude logged in users who haven't accessed the site in a while (i.e. maybe an hour of inactivity):
$timestamp = time - 3600; // 3600s is one hour.
$result = db_query('SELECT uid FROM {sessions} WHERE uid != 0 AND timestamp >= %d', $timestamp);
You might also want to limit how many users to return. For example, maybe you want to grab - at most - the last 10 logged in users who accessed the site:
$limit = 10; // Limit to the last 10 users.
$result = db_query_range('SELECT uid FROM {sessions} WHERE uid != 0 ORDER BY timestamp DESC', $timestamp, 0, $limit);
As an aside, if you're going to be using magic numbers (like $limit or the 3600s), you should make them persistent using variable_set(), variable_get(), and variable_del().
My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion.
The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response.
In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the argument is link_identifier of last operation. My operation with database is on $wpdb. How to extract the link_identifier from $wpdb to make sure mysql_insert_id work? Is there any other way to get the last-inserted-row id from $wpdb?
Thanks.
Straight after the $wpdb->insert() that does the insert, do this:
$lastid = $wpdb->insert_id;
More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page
This is how I did it, in my code
...
global $wpdb;
$query = "INSERT INTO... VALUES(...)" ;
$wpdb->query(
$wpdb->prepare($query)
);
return $wpdb->insert_id;
...
More Class Variables
I needed to get the last id way after inserting it, so
$lastid = $wpdb->insert_id;
Was not an option.
Did the follow:
global $wpdb;
$id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
Get the last inserted row id in WP like this.
global $wpdb;
$order = ['product_name'=>'Computer', 'os_system'=>'Linux'];
$wpdb->insert('wp_product_orders', $order );
$last_inserted_id = $wpdb->insert_id;
Something like this should do it too :
$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'table_name'");
$lastid = $last->Auto_increment;
just like this :
global $wpdb;
$table_name='lorem_ipsum';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY ID DESC LIMIT 1");
print_r($results[0]->id);
simply your selecting all the rows then order them DESC by id , and displaying only the first
Putting the call to mysql_insert_id() inside a transaction, should do it:
mysql_query('BEGIN');
// Whatever code that does the insert here.
$id = mysql_insert_id();
mysql_query('COMMIT');
// Stuff with $id.