intuitive-custom-post-order icon indicating copy to clipboard operation
intuitive-custom-post-order copied to clipboard

Doing WP_Query on Ajax Requests

Open piggy1979 opened this issue 7 years ago • 4 comments

If a wp_query is called via an ajax request on a post type with a custom order it will force the menu_order on the order by param as well as the order to ASC.

The reason being is on line 614 is_admin() does the check to see if the user is on the dashboard or not. Problem is an ajax call in wordpress will treat is_admin as true, if if the user is not even logged in. I have switched it up slightly so on line 614 its now.

if ( is_admin() && !wp_doing_ajax() ) {

This seems to be working for me. I can still order posts but can also pass custom order params via ajax.

piggy1979 avatar Jul 17 '17 17:07 piggy1979

Had this issue today. Thanks for this!

ghost avatar May 25 '18 07:05 ghost

I had the same problem!

Only instead of changing the plugin you can do this in the ajax function where you use Wp_Query:

// Set $_GET['orderby'] to whatever you like. 
$orderby = $_GET['orderby'] = 'title';

$query_args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => 20,
        'paged' => $paged,
        'orderby' => $orderby,
        'order' => 'ASC'
    );

$query = new WP_Query($query_args);

Intuitive Custom post order is checking if this $_GET['orderby'] is set before if makes the WP_Query changes. So this way you do not need to worry about plugin updates :D

macure avatar Oct 14 '19 13:10 macure

I had the same problem!

Only instead of changing the plugin you can do this in the ajax function where you use Wp_Query:

// Set $_GET['orderby'] to whatever you like. 
$orderby = $_GET['orderby'] = 'title';

$query_args = array(
        'post_type' => $post_type,
        'post_status' => 'publish',
        'posts_per_page' => 20,
        'paged' => $paged,
        'orderby' => $orderby,
        'order' => 'ASC'
    );

$query = new WP_Query($query_args);

Intuitive Custom post order is checking if this $_GET['orderby'] is set before if makes the WP_Query changes. So this way you do not need to worry about plugin updates :D

Thank you for this! Looked for a long time before figuring out it was a combination of ICPO and doing an ajax call that was overriding the orderby paramteres.

harryatkins avatar May 10 '22 15:05 harryatkins

@harryatkins is this still relevant and can you maybe create a PR with a fix?

timohubois avatar Jan 18 '24 22:01 timohubois