WP_Query Order Posts By Custom Meta Value Array Not in Codex

Question!

I have a different sort of post order I'm trying to return that I can't find anywhere with an answer. I'm using a wp_query to try to order posts by a list of meta values. However non of the standard orderby options do what I'm trying to achieve.

My meta values are stored as a comma list string like so:

1, 5, 10, 3, 21, 6

Then I grab those values and explode them to an array like so:

$banner_nums = get_option('banner_numbers');
$nums_array = explode( ',', $banner_nums );

I do a standard wp_query, but the catch is, I'm trying to order the display of these posts by the actual order in my $banner_nums variable, not by any normal wp_query orderby such as 'ID', 'date', 'title', 'meta_value', etc.

Display like so:

1st: post with meta_value = 1

2nd: post with meta_value = 5

3rd: post with meta_value = 10

4th: post with meta_value = 3

5th: post with meta_value = 21

6th: post with meta_value = 6

If, for example, I want to display the post with meta_value = 6 as the 2nd post I'd change my list to: 1, 6, 5, 10, 3, 21.

Here is my query that grabs my posts fine, I just can't get them to order correctly.

$slider_args = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    'nopaging' => true
);

$my_query = array();

foreach( $nums_array as $k => $v ) {
    $my_query[$k]['key'] = 'property_id';
    $my_query[$k]['value'] = $v;
    $my_query[$k]['compare'] = '=';
}

$slider_args['meta_query'] = $my_query;
$slider_args['meta_query']['relation'] = 'OR';

$slider_query = new WP_Query( $slider_args );

if ( $slider_query->have_posts() ) {
    stuff...
}

Printing my $slider_args looks like this, which looks fine:

[post_type] => property
[posts_per_page] => -1
[nopaging] => 1
[meta_query] => Array
    (
        [0] => Array
            (
                [key] => property_id
                [value] => 1
                [compare] => =
            )

        [1] => Array
            (
                [key] => property_id
                [value] =>  5
                [compare] => =
            )

        [2] => Array
            (
                [key] => property_id
                [value] =>  10
                [compare] => =
            )

        [3] => Array
            (
                [key] => property_id
                [value] =>  3
                [compare] => =
            )

        [4] => Array
            (
                [key] => property_id
                [value] =>  21
                [compare] => =
            )

        [5] => Array
            (
                [key] => property_id
                [value] =>  6
                [compare] => =
            )

        [relation] => OR
    )

I just can't figure out how to display the posts in the correct order of

1, 5, 10, 3, 21, 6

I hope I'm making sense? Any help is much appreciated.



Answers

Learned something new myself trying to solve this for you :)

Apparently as of WP 3.5, you can specify an 'orderby' parameter of 'post__in' when creating your WP_Query

https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

So essentially what I think you could do would be to snag the post IDs that have the meta values you need and put those IDs into an array in the same order that they're specified in your meta values array. Then if you do a WP_Query on that array, pass in the arg:

'orderby' => 'post__in'

Now, as another commenter pointed out, this is probably a fairly expensive operation, so you'd like want to cache the result to a transient and use that output instead of running this query each time the page is loaded:

https://codex.wordpress.org/Transients_API



Make your divs appear after all images are finished loading. The divs are the correct size on reload because at that point all the images are cached by your browser and the JavaScript can pick up the appropriate size.

In jquery change your ready to this:

$(window).on("load", function() { // weave your magic here. });

should fix it. If not.

Something like this should help as a rough example.

var img = new Image();
img.onload = function() { alert("Height: " + this.height); }
img.src = "http://path/to/image.jpg"


based on the structure of the example, it's likely that webCrtPartList is NOT the managed entity. You need to use the response of EntityManager.merge() to get the managed entity (the parameter to EntityManager.merge() will not [necessarily] be managed)



This video can help you solving your question :)
By: admin