If I wanted to get a list of users in WordPress with an exception of users, how would I do this? So to get a complete list of users I could write a query like:
$dont_include = array(1,5,9); // IDs not to include
$query = "SELECT * FROM wp_users WHERE ID > 0 AND ID != %d";
$data = array(
$dont_include
);
$query = $wpdb->prepare($query, $data);
$results = $wpdb->get_results($query);
You can do it easily with the WordPress function get_users and the exclude parameter:
$dont_include = array(1,5,9); // IDs not to include
$args = array(
'exclude' => $dont_include,
'orderby' => 'login',
'order' => 'ASC',
'fields' => 'all',
);
$users = get_users($args);
exclude - An array of IDs. Users matching these IDs will not be returned, regardless of the other arguments. It will be ignored if the include argument is set.
More details get_users
Related
I need to get the subscribers user details based on the selection in the filter for this i have tried the below code
$status = join('","',$status);
$distribusjon = join('","',$distribusjon);
$qrys = '
AND (
(utrop_usermeta.meta_key = "status" AND utrop_usermeta.meta_value IN ("'.$status.'")) OR
(utrop_usermeta.meta_key = "mepr_distribusjon" AND utrop_usermeta.meta_value IN ("'.$distribusjon.'"))
)
';
$users = $wpdb->get_results('SELECT DISTINCT(ID) FROM '.$wpdb->users.' u LEFT JOIN '.$wpdb->usermeta.' m ON m.user_id = u.ID WHERE 1 AND ID IN ( SELECT user_id FROM '.$wpdb->usermeta.' WHERE 1 '.$qrys.') AND meta_key = "utrop_capabilities" AND meta_value REGEXP "[[:<:]]s:10:\"subscriber\";"');
You can try this method.
List all users:
$users = get_users();
Filter for role (ex. subscriber):
$users = get_users('role=subscriber');
Exclude for id user:
$users = get_users('exclude=1');
And You can filter by WP_User_Query
$q = new WP_User_Query(
array(
'role' => 'Your_role',
'meta_query' => array(
array(
'key' => 'Your_custom_meta_key',
'value' => 'Your_meta_value',
'compare' => 'LIKE'
)
)
)
);
Thanks
I have here a code below that will insert products into the database in woocommerce :
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => $product->part_num,
'post_parent' => '',
'post_type' => "product",
);
$post_id = wp_insert_post( $post, $wp_error );
But, before inserting a new product, I want to check if a slug is existing in the database, else, I want to add a new one and append a number on the slug.
example input:
$new_url = sanitize_title('This Long Title is what My Post or Page might be');
output :
this-long-title-is-what-my-post-or-page-might-be
Now, I want to check if this slug is already existing in the database. If it is existing, I will append a number on the slug(just like what wordpress permalink is doing). If it is already existing, i want to output this as:
this-long-title-is-what-my-post-or-page-might-be-1
I want to add new product into the database together with this new slug.
Does anybody know?
Not tested but just written that code. Try this and let me know
function slug_exists($slug){
if(get_page_by_path( $slug, OBJECT, 'product' )){
return true;
}
return false;
}
$slug = 'your_slug';
$new_slug = $slug;
$c=1;
while(slug_exists($new_slug)){
$new_slug = $slug.'-'.$c;
$c++;
}
// $new_slug containes your desired slug
I'm using WordPress meta data to register clicks on images, to know which images each user has clicked - and also the total number of clicked images per user. The first part is fine, but I'm struggling to get the counter going, as it's returning a lower amount of meta data than what is actually there.
I have a custom post type gallerier and each gallery has a number of images. I'm using the meta key nedlasting, and I'm identifying each image individually by fetching the url.
Here is how I register clicks, after checking it isn't already:
// Add meta query if it doesnt already exist
function sjekk_nedlasting( $postid, $url, $dato) {
$brukerid = (string)get_current_user_id();
// Check if the image is downloaded previously
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $url),
'compare' => 'LIKE'
),
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// Perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
// If not already downloaded, register it
if ( empty( $nedl_ids ) ) {
$metaarray = Array(
'user_id' => $brukerid,
'url' => $url,
'date' => $dato
);
add_post_meta( $postid, 'nedlasting', $metaarray );
}
}
Then I'm trying to count those registered clicks using the following function:
// Count number of downloads for a single user
function tell_nedlastinger() {
$brukerid = (string)get_current_user_id();
$args = array(
'post_type' => 'gallerier',
'meta_query' => array(
array(
'key' => 'nedlasting',
'value' => sprintf(':"%s";', $brukerid),
'compare' => 'LIKE'
)
),
'fields' => 'ids'
);
// perform the query
$nedl_query = new WP_Query( $args );
$nedl_ids = $nedl_query->posts;
return count($nedl_ids);
}
The function returns a number, but always much lower than the actual amount of registered meta data/clicks. Anyone seeing a problem?
Edit: I'm pretty sure the problem is that I'm getting the total number of posts, not the total number of meta data entries/clicks - which more often that not is several per post. Any way around that?
I never found a solution to query meta data and get x results per post, so instead of using an array in a single key I split the post meta data into three keys. Then I used $wpdb for a custom query. The final code for the tell_nedlastinger() function:
$brukerid = (string)get_current_user_id();
global $wpdb;
$query = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return count($query);
You can improve the speed of this by using:
$brukerid = (string)get_current_user_id();
global $wpdb;
$count = $wpdb->get_row("SELECT COUNT(*) AS THE_COUNT FROM $wpdb->postmeta WHERE (meta_key = 'nedlasting_brukerid' AND meta_value = '$brukerid')");
return $count->THE_COUNT;
The speed is improved by only having to fetch one row from the database. Also note the use of:
FROM $wpdb->postmeta
This allows for a variable table prefix instead of assuming a table wp_ prefix.
$metaCount = count( get_comment_meta($postId,'key','value') );
I am desperately trying to programmatically get the post ID associated with a WooCommerce Subscription object. I am starting from the user id, and trying to request the database using the get_posts function. A call using the get_users_subscriptions works but the objects returned do not include their ID, only the associated order_id or product_id.
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions(
$current_user->ID
);
$subscription_posts = get_posts( array(
'orderby' => 'date',
'order' => 'ASC',
'numberposts' => 1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => 'shop_subscription'
) );
The get_post request is sadly returning an empty array. Do you see something wrong in my request ?
Thanks in advance,
I did not find an easy solution so I went with a SQL request based on the order_id contained in the subscription object I get using the call of:
$subscriptions = WC_Subscriptions_Manager::get_users_subscriptions(
$current_user->ID
);
The $wpdb call looks like the following:
$subscription_id = $wpdb->get_var(
"SELECT ID
FROM $wpdb->posts
WHERE `post_type`='shop_subscription'
AND `post_parent`=$first_order->ID
ORDER BY `post_date` ASC
LIMIT 1;"
);
If it helps someone, you're welcome !
I've achieved the same thing with the following code, if you are using the wp rest APIs, it will also add a property to the response:
add_action(
'rest_api_init',
function() {
// add _subscription_id to wc-order response
register_rest_field( 'shop_order', '_subscription_id', [
'get_callback' => function ($order) {
$subscriptionIds = wcs_get_subscriptions_for_order( $order['id'] );
foreach( $subscriptionIds as $subscriptionId => $subscription )
if($subscription->order->id == $order['id']) break;
foreach( $order['meta_data'] as $meta )
if ($meta->key === '_subscription_renewal') {
$metaSubscriptionId = $meta->value; break;
}
return $subscriptionId ?: (int) $metaSubscriptionId;
},
'update_callback' => null,
'schema' => null,
]);
}
);
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 );