Wordpress - $query, how to order users by lastname - php

I am trying to change a query that gets users from the db and orders by user_nicename. I would like to sort the order of the users by surname. I have tried changing the ORDER BY the meta data value of last_name, but this didn't work.
Here is the current bit of code that I am trying to change:
global $wpdb;
$query = "SELECT ID, user_nicename from $wpdb->users ORDER BY user_nicename";
$author_ids = $wpdb->get_results($query);
Any tips or help would be greatly appreciated. I am new to PHP and and learning, but have found myself stumped by this problem.
Many thanks

This will enable you to retrieve users orderby their lastname.
$args = array(
'meta_key' => 'last_name',
);
$wp_user_query = new WP_User_Query($args);
$wp_user_query->query_orderby = str_replace( 'user_login', 'wp_usermeta.meta_value', $wp_user_query->query_orderby );
$wp_user_query->query();
$users = $wp_user_query->get_results();

Related

Is there a way to change a text in a Wordpress plugin?

I've installed a paid plugin called Zipcode checker. But sadly there is no support with translations. The developer of the plugin is telling me they don't have the functionality for language changing. I tried Loco Translate and it didn't work. WPML did partly the job.
It worked to translate some words in the code of this plugin. There is one more word that I am unable to translate in the code.
When I use the zipcode checker on the site (on the right in the menu) and I use as example 8494 it gives me results. The word YES is what i want to translate to Dutch= JA. I tried but it's not working.
I hope this is the right code:
$table_name = $wpdb->prefix . "zip_code_checker";
if ( !empty($creds['zip_code']) ) {
$results = $wpdb->get_results( 'SELECT * FROM '.$table_name.' WHERE zip_code = "'. $creds['zip_code'] .'" AND company = "FedEx" AND status IN( "Yes","yes" ) ORDER BY id ASC LIMIT 1', ARRAY_A );
if(empty($results)) {
$results = $wpdb->get_results( 'SELECT * FROM '.$table_name.' WHERE zip_code = "'. $creds['zip_code'] .'" AND company != "India Post" AND status IN( "Yes","yes" ) ORDER BY id LIMIT 1', ARRAY_A );
}
if(empty($results)) {
$results = $wpdb->get_results( 'SELECT * FROM '.$table_name.' WHERE zip_code = "'. $creds['zip_code'] .'" AND company = "India Post" ORDER BY id LIMIT 1', ARRAY_A );
}
}
The url of the website is : https://graphicfish.nl/kaasboersiebren/
I know that updates of the plugin will erase my changes.
Can somebody help me?

WordPress "s" query with a few keywords in $args

I have this query that works fine:
$query = new WP_Query(array( 's' => $keyword ,'post_status' => array('publish', 'pending', 'draft')) );
Its searches and finds the post with the keyword in title.
I want to query posts with a few keywords at once, (Now i'm doing a loop over all keywords and query each keyword by itself.
If it was mysql it would have be something like:
Select * from ..... where title='keyword1' or title='keyword2' or title='keyword3' ;
How could achieve that with WP_Query?
You can't do it using the WP_Query directly.
An alternative for looping the WP_Query is use the $wpdb.
$wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE where title='keyword1' or title='keyword2' or title='keyword3'" );
U have to use $wpdb (#codex)
$query= $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM ....
WHERE title= %s OR
title=%s OR
title = %s
",
$keyword1,
$keyword2,
$keyword3
) );
I hope that help u:)

Get user id from wp_usermeta table

I have stored the certain term ids in wp_usermeta table.
i want to get the user id where the term id match with the user meta key 'location' value.
i have tried it with below query but it is doing nothing.
global $wpdb;
$term = get_queried_object();
$query = $wpdb->get_results("SELECT user_id FROM wp_usermeta WHERE meta_key='location' AND meta_value LIKE %s",'%'.$term->term_id.'%');
try this, you are not passing the value correctly
$query = $wpdb->get_results("SELECT user_id FROM wp_usermeta WHERE
meta_key='location' AND meta_value LIKE '%s,%".$term->term_id."%'");
Is there any reason for using a sql query? i think this will do what you want exactly :
$user_query = new WP_User_Query( array( 'meta_key' => 'location', 'meta_value' => 'ANY' ) );
foreach ($user_query as $user) {
$user->ID; // the the user id
}
I have figured it out. Here is my solution. if someone needed.
$term = get_queried_object();
$wpdb->get_results($wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key='location' AND $wpdb->usermeta.meta_value LIKE %s", '%' . $term->term_id . '%'));

Getting a single MYSQL result where the field is an array

Wordpress database, bit stuck on this one.
I'm using the following to get the ID of the current user.
$user_ID = get_current_user_id();
This returns something like this :
15
Now I try to find the matching value of $user_ID in the field show_user_list The data in this field is stored in an array.
Which looks something like this :
a:2:{i:0;s:2:"29";i:1;s:2:"15";}
This is the query i'm running (along with a set of conditions) :
global $wpdb; $result = $wpdb->get_results( "SELECT post_id FROM wp_postmeta WHERE show_user_list IN (' . implode(',', $user_ID) . ' AND post_type = 'show' AND post_status = 'publish'" );
And then I'm trying to echo the value of the matching post_id with this :
foreach ( $result as $unique ) {
echo $unique->post_id;
}
But it's not returning anything. I know I must be making a mistake while dealing with the array but I don't know where I'm going wrong?
looks like you have stored a serialized array in the show_user_list field, so it will be a hustle to search for values into using a db query.
In the model you described, you have to select all the rows from wp_postmeta that match "post_type = 'show' AND post_status = 'publish'", then manually filter results that do not have the user id in the unserialized show_user_list field.
You might try something like :
in_array($user_ID, unserialize($row->show_user_list))
Also, I noticed multiple errors in your query: string not properly concatenated with PHP code and the right parenthesis of the IN clause not closed.
Regards,
same
EDIT
Here is how I would solve your problem providing info you have given :
$user_ID = get_current_user_id();
global $wpdb;
$results = $wpdb->get_results("SELECT post_id, show_user_list FROM wp_postmeta WHERE post_type = 'show' AND post_status = 'publish'");
$user_post_ids = array();
foreach ($results as $post) {
if (in_array($user_ID, unserialize($post->show_user_list))) {
$user_post_ids[] = $post->post_id;
}
}
Hope this helps !

WordPress custom SQL

I have this function which contains some custom SQL:
function user_comment_count_by_meta( $user_id, $meta_key )
{
global $wpdb;
$count = 0;
$sql = "SELECT count(*) FROM $wpdb->comments comments INNER JOIN $wpdb->commentmeta meta ON comments.comment_ID = meta.comment_id WHERE comments.user_id = %d AND meta.meta_key = %s";
$count = $wpdb->get_var( $wpdb->prepare( $sql, $user_id, $meta_key ) );
return $count;
}
What it should be doing is counting all the comments for a user that have a particular meta value attached to them and returning that number. So for example if a user has made 20 comments and then 11 of those have the meta value 'accepted' attached to them then the number returned would be 11.
I call the function like so:
<?php $count = user_comment_count_by_meta( get_the_author_meta('id'), 'accepted' ); ?>
However it doesn't return anything. Not sure where I have gone wrong? If any SQL geniuses could help or if anyone can spot a problem it'd be much appreciated. Thanks.
Well,I think that the SQL it's ok, but when you call your function you are using
get_the_author_meta('id')
and this function I think that have other meaning.
If you want the ID from the post author you must use:
get_the_author_ID()
I'm not sure. really.

Categories