Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Order by Number in Meta Value with Link WordPress

  • REFUNDED

I am trying to order my customs fields by ranking my meta_value. The values all have a link with them so when I try to order it it does not work.

My current code:
ORDER BY ABS(wpostmeta.meta_value) DESC

Inside the meta_value is written as such:
<blockquote><a href="http://www.example.com/">100000</a></blockquote>

And another meta_value within the custom field would be:
<blockquote><a href="http://www.example.com/page2/">150000</a></blockquote>

And the final meta_value within the custom field would be:
<blockquote><a href="http://www.example.com/page3/">200000</a></blockquote>

And so on.

I want it displayed as:
200000
150000
100000

with the links to the pages included.



Answers (3)

2013-09-28

Arnav Joy answers:

Do you have these meta values in the same post and want to display it or these values are in different posts?


vitaminman comments:

The meta values are on seperate posts.

Here is the entire code for that section:
<?php
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'number'
AND wposts.post_type = 'page'
ORDER BY ABS(wpostmeta.meta_value) DESC

";

$pages = $wpdb->get_results($querystr, OBJECT);
foreach($pages as $page)
{ ?>

2013-09-28

Balanean Corneliu answers:

How about you give CAST() a try in the ORDER BY clause ([[LINK href="http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast"]]dev.mysql.com/doc/refman/5.0/en/…[[/LINK]]). Something like ORDER BY CAST(wpostmeta.meta_value AS DECIMAL) DESC


Balanean Corneliu comments:

Or you can verify the long way to :

$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'price'
AND wposts.post_type = 'post'
ORDER BY wpostmeta.meta_value+0 DESC
";


vitaminman comments:

Tried it. Didn't work.


Balanean Corneliu comments:

Can you give your link?
Test this one to $args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'rating',
'value' => 3,
'compare' => '>'
)
),
'orderby' => 'rating post_date',
'order' => 'DESC'
);
$query = new WP_Query( $args );


Balanean Corneliu comments:

Do you have a symbol before the numbers?


Balanean Corneliu comments:

On the last code you can change the value 'rating' with what you whant.


vitaminman comments:

I don't have a symbol.

This is the entire code that I have.

<?php
global $wpdb;
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id
AND wpostmeta.meta_key = 'number'
AND wposts.post_type = 'page'
ORDER BY ABS(wpostmeta.meta_value) DESC

";

$pages = $wpdb->get_results($querystr, OBJECT);
foreach($pages as $page)
{ ?>



vitaminman comments:

And inside the meta values are:

<a href="http://www.example.com/">100000</a>

<a href="http://www.example.com/page1/">200000</a>

And so on.

2013-09-28

Eric P. answers:

Are you saying the meta values are exactly '<a href="http://www.example.com/">100000</a>'

The meta values are strings that include the <a..>..</a> tags, and you want to sort by the number only.

I suspect it's sorting by the URL now, as that's the first difference in the string and that's what it will sort by.

You probably want to store <em><strong>only</strong></em> the number in the meta value, then sort by that using one of the suggested methods (CAST or (value + 0) or (value * 1) to force it to be a number).


Eric P. comments:

If the links are the permalink from the post where the meta is, you can just put the number in the meta "custom field" and when you render, render something like:


<a href="<?php the_permalink; ?>"><?php echo $post_meta_value; ?></a>


This assumes you placed the appropriate meta data in $post_meta_value. Or use whatever is appropriate in the echo.

If your link is variable and separate from the permalink, use two post meta custom fields. Let's say your query puts those into $post_meta_link and $post_meta_value. Then the code to render would be:


<a href="<?php echo $post_metal_link; ?>"><?php echo $post_meta_value; ?></a>


Storing entire links with the whole <a ...>...</a> tags in a custom field will make it much more difficult to sort by the text of the link.

If you store only the value (if the link is the permalink of the post/pate), or if you store the link and value separately, then you can sort by the value easily.