Wordpress descending order putting 10 after 1 instead of 2 - php

So, I have a custom field type called "postorder." I added some php to the template page (see below) to call in the "postorder" for each of the posts, and to sort them in descending order. The problem I am having is that Wordpress is putting 10 after 1. So, the order of my posts is 1,10,2,3,4,5,6,7,8,9. I would like for 10 to come after 9, or find a different value to order my posts.
query_posts('showposts=1000&meta_key=postorder&orderby=meta_value&ASC&post_type='.portfolio);

WordPress is treating the values as string to sort, so 10 comes after 1, just change following
orderby=meta_value
to (values will be treated as number)
orderby=meta_value_num
When sorting by number, use meta_value_num instead of meta_value to make WordPress treat the value as a number instead of a string.

Related

Excel - Getting the 2nd or nth matched string from your corresponding data

With my previous posts
1. PHPSpreadsheet generates an error "Wrong number of arguments for INDEX() function: 5 given, between 1 and 4 expected"
2. Excel - Getting the Top 5 data of a column and their matching title but produces duplicates
I have found out that the PHPSpreadsheet library for PHP is yet to allow the usage of the AGGREGATE() and complicated formulas/functions but I'm in dire need of their functionalities
Going back, I have 2 columns in my Excel (produced by my web applications made from CodeIgniter and Laravel)
The problem is, the Article Count column (on the right) contains 2 values of 54 which is supposed to belong to 2 different Publications (on the left) but with the use of the formula =INDEX(E$4:E$38,MATCH(M4,J$4:J$38,0)) it just fetches the 1st matched Publication.
The output should look like this:
The original Table:
My question is, what would be the right function or code in Excel so I could retrieve the SECOND Publication of my matched data?
I'm aiming to target those Publications that has the Article Count of 54, but I want to aim the SECOND ONE which is the letter D WITHOUT using the Aggregate() function of Excel
Here are my used codes
1) =LARGE(J4:J38,1) - J4:J38 is my range of raw data, I am using this to get the 5 highest numbers in descending order
2) =INDEX(E4:E38,MATCH(M4,J4:J38,0)) - I'm using this to retrieve the Publication Names that matched the Article Count
After communicating in chat, we got this correct formula:
=INDEX(E$2:E$38,IF(M4=M3,MATCH(L3,E$2:E$38,0),0)+MATCH(M4,OFFSET(J$2,IF(M4=M3,MATCH(L3,E$2:E$38,0),0),0,COUNT(J$2:J$38)-IF(M4=M3,MATCH(L3,E$2:E$38,0),0),1),0))
How this works:
This IF(M4=M3,MATCH(L3,E$2:E$38,0),0) returns the position of the previous row's publication title in the titles array (E), in case the current publication count is the same with the previous one. Let's call this number X. Instead of using J2:J38 for the results, we use J(2+X):J38. This trick is done by using offset to cut off the previous section, already used by the previous row. This way, on repeating publication counts the already mentioned titles get ignored.
You need to use AGGREGATE's SMALL sub-function to return the smallest matching row number and adjust the k argument to accommodate duplicate rankings.
'in M4
=LARGE(J$4:J$38, ROW(1:1))
'in L4
=INDEX(I:I, AGGREGATE(15, 7, ROW($4:$38)/(J$4:J$38=M4), COUNTIF(M$4:M4, M4)))
enter image description here

MySQL sorting date then by part number

Essentially I want these parts (below) grouped then the groups place in order of time, starting from the latest time being at the top of the list.
ID Parts Time
1 SMH_2010 08:59:18
2 JJK_0101 08:59:26
3 FTD_0002 08:59:24
4 JJK_0102 08:59:27
5 FTD_0001 08:59:22
6 SMH_2010 08:59:20
7 FTD_0003 08:59:25
So, the results would look like:
ID Parts Time
1 JJK_0101 08:59:26
2 JJK_0102 08:59:27
3 FTD_0001 08:59:22
4 FTD_0002 08:59:24
5 FTD_0003 08:59:25
6 SMH_2010 08:59:20
7 SMH_2010 08:59:18
Please, I would be grateful for any help.
What you are asking is not sorting in the traditional meaning. Your first attempt orders the result by time, and then by part if multiple timestamps occur at the same time.
What you want neither sorts the result in alphabetically by Parts name, nor ascending/descending on timestamp. What you are asking for can't be accomplished by the sort operation in SQL. Having the parts in sequence is not ordering.
I finally found a solution to this. Not my ideal solution but, never the less it works.
I added another field called max_date which by default is ‘now()’ as every new part is inserted.
I create a prefix from the current part being inserted, something like “SMH_” as a variable called $prefix = “SMH_”;
I have another query that directly follows the insert, which updates the max_date again, by ‘now()’ where the prefix is like $prefix.
UPDATE parts SET max_date = now() WHERE prefix LIKE '%$prefix%'
To display the results I use something along the line of :
SELECT * FROM parts ORDER BY parts.max_date DESC, parts.part ASC

PHP pagination depending on table's column value in database

I need to do pagination for table contain the following:
id.
name.
type (this contains: bad, good, great).
I need to order the result depending on type, first to appear 'great' then 'good' and last 'bad'. For example: I have 36 rows with type 'great', 25 with type 'good' and 13 with type 'bad' (total number of rows is 74), and I'm going to display 10 at each page. So at page 4 there will be 6 of type 'great' and 4 of type 'good'.
How to do such pagination?
On click of a button (arrow, , submit input, whatever suits you) send a parameter via get or post with the page number (starting with 0).
On your server side get the page number that you sent, check if it's valid and clean it off hazardous stuff (like injections of all sorts). Multiply it by some "pagesize" and then query the database for a range of records (order by type, id).
Example: 74 records, page 4, you pass page=3 as GET parameter and set it to some variable ($page), multiply it by page size and put into a query with offset and limit:
SELECT * FROM table
ORDER BY type DESC, id
LIMIT 10 OFFSET 30
where offset is calculated as $page * $pageSize, and limit is $pageSize. As a result you get 10 records from the range 31-40, sorted by type.
In this concrete example you can just alphabetically sort your types descending (since 'great' > 'good' > 'bad'), but you might need more advanced sorting for more different types.

Get highest custom field value from last x months

in Wordpress, I've got a category called "Reviews" where I assign a vote (in a custom field called 'rate') to every single post I create in this category.
Now I'm trying to get the review with the highest rate in the last X months.
The rate ranges from 0 to 10, with one decimal number (ex. 5.3, 7.4, 9.1).
How can I do this? Use WP_Query or direct DB query? Thanks in advance.
You can use something like this to get the highest rating:
SELECT max(cast(meta_value as unsigned)) FROM wp_postmeta WHERE meta_key='your_meta_name'
But post meta data does not hold the date/time value for you. You need to think a way to insert date value as well.

Sort MySQL in Human

here's the code:
$sql_namesResult = mysql_query("SELECT name FROM `scrimaprovedlist` ORDER BY `scrimaprovedlist`.`eorank`");
eo rank is a NUMERICAL value for a rank (general, colonel, ect).
The problem is, when i set myself for 1, i am the top, but comes rank 10, instead of rank 2. how do i edit this to make it show in order:
1
2
3
10
20
30
I'm currently using "rank" instead of "eorank" because it is easier. but the problem is i have to manually edit the ranks over and over again so that they show in the correct order. Any ideas?
Viewable at http://www.thexcrew.com/modules.php?name=Roster
ORDER BY CAST(scrimaprovedlist.eorank AS INTEGER)
Your ranks are strings instead of integers so they will be sorted as a string unless you cast or convert them to integers which I've done above
figured out a way, i changed my Numerical value to an alphabetical value. using only 17 ranks, i am able to substitute #'s for letters. thanks for the help anyway :)

Categories