最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Sort by custom field that is an array?

programmeradmin1浏览0评论

is it possible to sort posts inside a query based on post value that is in an array - but using the last element of the array?

The code looks something like:

<?php

$mostsharedposts= new WP_Query( 
        array( 'posts_per_page' => 10,
               'meta_key' => 'mashsb_shares',
               'orderby' => 'meta_value', 
               'order' => 'DESC' ) );

while ( $mostsharedposts->have_posts() ) : $mostsharedposts->the_post();

/* Loop here e.g.:*/

endwhile;
?>

meta_value is an array, and I just need the last element of that array. Can I somehow extract that element in a variable, using explode() and end() and sort by that variable or ...? Any idea would be helpful :)

is it possible to sort posts inside a query based on post value that is in an array - but using the last element of the array?

The code looks something like:

<?php

$mostsharedposts= new WP_Query( 
        array( 'posts_per_page' => 10,
               'meta_key' => 'mashsb_shares',
               'orderby' => 'meta_value', 
               'order' => 'DESC' ) );

while ( $mostsharedposts->have_posts() ) : $mostsharedposts->the_post();

/* Loop here e.g.:*/

endwhile;
?>

meta_value is an array, and I just need the last element of that array. Can I somehow extract that element in a variable, using explode() and end() and sort by that variable or ...? Any idea would be helpful :)

Share Improve this question asked Oct 11, 2014 at 13:26 idjuradjidjuradj 1853 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

At the stage of retrieving posts from database this is impossible (or at least impractical), since arrays are stored serialized by PHP in meta table and SQL won't be able to process them on the fly effectively.

This is easy enough to do for specific set of posts retrieved, just sort results in $mostsharedposts->posts before output. But that won't give you sane continuous pagination in that order.

In a nutshell if you want this in reliable non–hacky way, you will likely want to break out copy of that value in array into separate meta field.

发布评论

评论列表(0)

  1. 暂无评论