I have wp_users table which has a column ordering. I came to know that get_users() returns all the users.
I am using it like get_users('orderby=ordering')
I got help form this link
But unfortunately it is not sorting on ordering column.
Any help?
You should first take a look at the users table from the database.
The command you try is good, but the argument you use for ordering might be wrong. You should order by a column from the users table, for example user name, or user id's..
On the link you mentioned I've found these:
orderby - Sort by 'ID', 'login', 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'.
order - ASC (ascending) or DESC (descending).
Some working examples:
Get users by nicename:
$users = get_users('orderby=nicename');
Other examples:
Display users sorted by Post Count, Descending order
$user_query = new WP_User_Query( array ( 'orderby' => 'post_count', 'order' => 'DESC' ) );
Display users sorted by registered, Ascending order
$user_query = new WP_User_Query( array ( 'orderby' => 'registered', 'order' => 'ASC' ) );
Related
I am using WordPress platform, and using WP_Query to call my data although can use custom MYSQL script if if can work.
I just want to add a where condition which can filter JSON value as below explained:
so in postmeta table i have a following columns
meta_key: _stock_reserve_outlet_extended
meta_value: a:2:{s:8:"outlet_2";a:2:{s:5:"stock";s:2:"20";s:5:"rider";s:2:"-1";}s:8:"outlet_1";a:2:{s:5:"stock";i:-4;s:5:"rider";i:0;}}
which is actually an array extracted as below by get_post_meta() function:
Array (
[outlet_2] => Array (
[stock] => 45
[rider] => 0
)
[outlet_1] => Array (
[stock] => -4
[rider] => 0)
)
)
i want to filter rows during calling rows from script by comparing following:
stock > 0 in outlet_2
I tried to search filter json values filtration by json_search but that does not compare by key.
To give WP_Query a try, i use following but nothing effect shows
$args['meta_query'][] = array(
'key' => '_stock_outlet_extended',
'value' => array(
'key' => 'stock',
'value' => '18'
),
'compare' => '>',
);
Then i tried following script but that not work aswell:
SELECT meta_value
FROM `frbl1ozme_postmeta`
WHERE `post_id` = 3699 AND `meta_key` = '_stock_outlet_extended' AND meta_value ->'$.stock' > 1;
Do anyone know how to tackle this sort of criteria?
what i observer is if you are saving any array by using update_post_meta() function of wordpress then it will serialize it and you are unable to filter rows by script then.
So after understood this fact i made the changes in structure and along with the stock i am saving status according to the value condition of stock, so i can filter rows easily by MYSQL.
So in one line answer, one can not achieve filter serialize rows via mysql queries till date.
Thank you all for the Help, specially #RiggsFolly
I have write query to get distinct stateID whose status is active, from tbl_summer table which is primary key of table tbl_states.
I want the listing of distinct state names in alphabetical order.
Actually i got this from following query but alphabetical order is not getting...
So what is the solution...?
Here is my query :
$query = Tbl_summer::find()
->select('tbl_summer.StateID, tbl_states.state_name')
->distinct('tbl_summer.StateID')
->from('tbl_summer')
->leftJoin('tbl_states', ['tbl_states.ID' => 'tbl_summer.StateID'])
->where(['tbl_summer.IsActive' => '1'])
->orderBy(['tbl_states.state_name' => SORT_ASC]);
Does this work?
$query = Tbl_summer::find()
->select('tbl_summer.StateID, tbl_states.state_name')
->from('tbl_summer')
->leftJoin('tbl_states', ['tbl_states.ID' => 'tbl_summer.StateID'])
->where(['tbl_summer.IsActive' => '1'])
->groupBy('tbl_summer.StateID, tbl_states.state_name')
->orderBy(['tbl_states.state_name' => SORT_ASC]);
I think the second field in groupBy is not needed if there is only one name for one id.
Good afternoon.
I have a model called 'Cliente' and another called 'Acct'. The ratio is 1 'Cliente' for many 'Acct'. When I use a has_one relationship, it fetches all the millions of 'Acct' to pick only one of these results.
Statement of the model in relation 'Cliente':
'accts' => [
self::HAS_MANY,
'Acct',
'cliente_id',
],
'lastAcct' => [
self::HAS_ONE,
'Acct',
'cliente_id',
'order' => 'acct.id DESC',
],
In Yii (Yii1 as well as Yii2), creating a "Has One" relationship does not automatically apply a LIMIT 1 to the query. You can read more about the reasoning behind it here: https://github.com/yiisoft/yii/pull/2113
You should manually add the limit clause, like so:
'lastAcct' => [
self::HAS_ONE,
'Acct',
'cliente_id',
'order' => 'acct.id DESC',
'limit' => '1'
],
Thanks for your answer, sir.
This 'limit' to enter the main query and not only in relation, for example, if you search thousands of 'clients' with the last 'acct' in each will not work, will get only one 'cliente'.
Complementing ...
To solve this I use a subquery, for example:
LEFT OUTER JOIN `radacct` `acct` ON ((acct.username = t.login) AND (acct.radacctid = (SELECT radacctid FROM `radacct` `acct_subquery` WHERE acct_subquery.username = t.login GROUP BY acct_subquery.username ORDER BY acct_subquery.radacctid DESC LIMIT 1)))
But what worries me is that after this subquery be too cool with millions of results, it is a must do and ERP reports, weekly, monthly, yearly and since he started the company.
So I have a model FeaturedListing that has a field date which is a mysql date field. There will be multiple FeaturedListings that have the same date value.
I want to find all dates that have N or more FeaturedListings on it. I think I'd have to group by date and then find which ones have N or more in there group and get the value that was grouped on. Could any one give me any pointers to accomplish that. A raw sql query may be required.
Edit: Thanks to the answers below it got me going on the right track and I finally have a solution I like. This has some extra conditions specific to my application but I think its pretty clear. This snippet finds all dates after today that have at least N featured listings on them.
$dates = $this->find('list', array(
'fields' => array('FeaturedListing.date'),
'conditions' => array('FeaturedListing.date >' => date('Y-m-d') ),
'group' => array('FeaturedListing.date HAVING COUNT(*) >= $N')
)
);
I then make a call to array_values() to remove the index from the returned list and flatten it to an array of date strings.
$dates = array_values($dates);
No need to go to raw SQL, you can achieve this easily in cake ($n is the variable that holds N):
$featuredListings = $this->FeaturedListing->find('all', array(
'fields' => array('FeaturedListing.date'),
'group' => array('FeaturedListing.date HAVING COUNT(*)' => $n),
));
In "raw" SQL you would use group by and having:
select `date`
from FeaturedListings fl
group by `date`
having count(*) >= N;
If you want the listings on these dates, you need to join this back to the original data. Here is one method:
select fl.*
from FeaturedListings fl join
(select `date`
from FeaturedListings fl
group by `date`
having count(*) >= N
) fld
on fl.`date` = fld.`date`
I'm in the view action of my PhotosController.php. What I want to do is given the id of the current photo I am viewing, create a carousel of photos containing the two photos before and two photos after the current photo with the current photo in the middle (5 in total).
I was pointed to this solution but I can't seem to convert it to CakePHP using $this->Photo->query.
My controller
$this->set('photos', $this->Photo->query("
SELECT id, file FROM photos WHERE id <= $id AND page_id = $page_id ORDER BY id DESC LIMIT 3
UNION ALL
SELECT id, file FROM photos WHERE id > $id AND page_id = $page_id ORDER BY id ASC LIMIT 2
"));
Unfortunately, when I don't see anything when I turn debugging on. id, file, and page_id are all columns in the photos table. Both #id and $page_id are passed to the action from the router. Is my syntax wrong?
EDIT: If I remove the UNION ALL and the second SELECT statement, then the query works fine so it's not an issue with the model not being loaded because it is.
EDIT (workaround): For now I'm doing two queries which is not ideal.
$this->set('photos_before', $this->Photo->find('all', array(
'conditions' => array(
'Photo.page_id' => $page_id,
'Photo.id <' => $id
),
'order' => array('Photo.id ASC'),
'limit' => 2
)));
$this->set('photos_after', $this->Photo->find('all', array(
'conditions' => array(
'Photo.page_id' => $page_id,
'Photo.id >' => $id
),
'order' => array('Photo.id ASC'),
'limit' => 2
)));
I have a contain before hand to only return the fields and associated models I need.
Below is what I want to be displayed and it currently works using the two queries above but I am hoping this can be achieved with a single, Cake-friendly query
My guess is that your original query is invalid SQL. Afaik UNIONS cannot contain multiple 'order by' clauses. As a workaround you may consider to rewrite it to use subqueries like this:
SELECT * FROM (SELECT id, file FROM photos WHERE id <= $id AND page_id = $page_id ORDER BY id DESC LIMIT 3) AS suba
UNION ALL
SELECT * FROM (SELECT id, file FROM photos WHERE id > $id AND page_id = $page_id ORDER BY id ASC LIMIT 2) AS subb
Although I serious think a query like this is far from optimal. Of course, I don't know the way your application works, but it seems that a standard pagination query, with a OFFSET/LIMIT is a more logical approach.
Please take my comment below your question into account; using model->query does NOT automatically handle sanitisation/escaping to prevent SQL injections!
You have to load model as
$this->loadModel('Photo');
Before executing query.
You should create a VIEW in MySQL and then use that as a model, and do a traditional CakePHP find on that.
Read up on creating views in MySQL and then create a model based on that view name.