0

I've created an array based on the numerical values of an object. After which I removed everything in the object in the array, except for the ID. So it ends up looking like this:

[115, 123, 66, 64]

That is my array of posts in the order I want it in.

I then want to display those posts in that order using the Ajax Load More Plugin. So I set it up as such:

obj['post__in'] = distanceIdFilter,
obj['orderby'] = 'post__in'

Note that the 'distanceIdFilter' is just the variable for the array I would like to use. When I console log it the post__in is grabbing the correct array and the correct order I would like it to be in. orderby also says that it is ordering by "post__in" which is also what I want because it should show the items in the array in that order.

The problem comes from when I filter that with Ajax Load More, it doesn't put the posts in the correct order and I assume it is putting them in order by date, because of the default orderby parameter.

I can't seem to figure out why it is not being ordered correctly?

  • I would suggest to debug $wp_query object of your query to see if post__in was used at all. This setting is pretty straightforward. – Butuzov Apr 3 '18 at 16:23
0

If you look at the source of the Ajax Load More plugin, you can see a number of filters you can use to modify the query. You'll likely want to use the alm_modify_query_args filter like so:

$in_array = [ 115, 123, 66, 64 ];
add_filter( 'alm_modify_query_args', function( $args ) use ( $in_array ) {
  $args[ 'post__in' ] = $in_array;
  $args[ 'orderby' ]  = 'post__in';
  return $args;
} );

Had the developer not been so nice and provided this filter, you could have always used the pre_get_posts filter.

  • I tried going to the source link, but it didn't want to seem to load for me. I also would love to try and use your code snippet, but the way I am doing it is via javascript, not php. Do you know of a way of doing the same thing, but with javascript? – Logan C Apr 4 '18 at 17:22
  • There's no way to do this with a 100% JavaScript solution since the way WordPress queries the database is though PHP. – Nathan Johnson Apr 4 '18 at 17:32
  • I'm not doing it 100% with Javascript I'm creating the query with javascript, but then using Ajax Load More plugin to make the php call to query the database. Does that change it so another solution could work? I'm doing it using this method: connekthq.com/plugins/ajax-load-more/examples/filtering/… – Logan C Apr 4 '18 at 17:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.