Apply Sort Order to custom query
-
I’m trying to use your plug-in with a custom WP_Query() on a custom posttype. Is that something that should work or be possible?
Here is my query:
$args = array( 'post_type' => 'menu-item', 'posts_per_page' => -1, 'parent' => $cat_id, 'tax_query' => array( array( 'taxonomy' => 'menus', 'field' => 'ID', //can be set to ID 'terms' => $cat_id, //if field is ID you can reference by cat/term number 'include_children' => false ) ) ); $menu_items = new WP_Query( $args );-
This topic was modified 8 years, 6 months ago by
maccast.
-
This topic was modified 8 years, 6 months ago by
-
yes, it will. The plugin hooks the
posts_whereand theposts_orderbyfilters to ensure the query returns the posts in your custom order. However, these filters are not fired if your query uses the attribute ‘suppress_filters=trueor if you use theget_postsfunction which by default sets the attribute 'suppress_filters=true.Ok, well I’m maybe a bit confused then because you can see from my code above that I’m using
WP_Queryand I am NOT settingsuppress_filters=true, but the results I’m getting are not being sorted in the custom order. Do you know why that might be?sorry for not replying earlier, I was travelling.
the results I’m getting are not being sorted in the custom order. Do you know why that might be?
that’s odd. Can you confirm that your posts are being sorted using the default query?
I have the same problem.
I noticed that the plugin code relies on get_queried_object() to get the category id etc. Is it possible that this won’t work when not on a taxonomy archive page?
if you are using a custom query it should not matter, and still work.
Can you please debug the SQL query being executed to see if the
JOINstatement is being appended. It should have anNNER JOIN on wp_reorder_post_rel...appended on the sql query.If this ain’t the case, please post more details of your query and what it is you are trying to achieve on your page.
Not sure exactly what I’m looking at, buthere is the full SQL query I’m seeing:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_reorder_post_rel ON wp_posts.ID = wp_reorder_post_rel.post_id and incl = 1 WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (29) ) AND wp_posts.post_type = 'menu-item' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') AND wp_reorder_post_rel.category_id = '29' GROUP BY wp_posts.ID ORDER BY wp_reorder_post_rel.id ASC LIMIT 0, 1There is an
INNER JOIN wp_reorder_post_rel ON wp_posts.ID = wp_reorder_post_rel.post_id, but when the page is rendered the items are not displayed in the custom order.-
This reply was modified 8 years, 4 months ago by
maccast.
Did u try to run the sql query in a phpmyadmin session to see if it returns the correct order?
Yes, but as you can see it wouldn’t work because for some odd reason it has a
ASC LIMIT 0, 1on it. Meaning there is nothing to “order” since it’s only one result. That’s why i=I wasn’t sure it was the correct query. If I remove thelimitit does appear to return the correct items.-
This reply was modified 8 years, 4 months ago by
maccast.
am afraid you don’t understand mysql order by / limit syntax.
The sql statement returns only 1 row from the ordered result, starting from the first (starts at 0) row.
This is exactly what you want when you do a front-end query as you want to loop over the results 1 row at a time (or 1 post at a time). Have you forgotten to loop through your results?
If I remove the limit it does appear to return the correct items.
which means the plugin is working as expected, so I am closing this thread. If you need more help understand custom queries in WordPress, I suggest you post on the general forum or on stackexchange.
-
This reply was modified 8 years, 4 months ago by
Aurovrata Venet.
Hey, thanks for the reply and the explanation. I do understand the mysql order/limit syntax. Sorry, I just really didn’t have enough background with the code to understand how it was working in this context. Yes, I am looping over my results, but the issue is those results, at least for me, still don’t seem to be sorted in the correct order. It is entirely possible I’ve made an error someplace else in my code and I will look into this. I really appreciate your help. Thank you.
You’re welcome.
Did you try to implement a loop to see if the results come in order? Please see this example.
Hi there,
I am running into a similar issue with my query on a custom post. I check the request that is being made and the left join is not being added to the query yet I have all the correct settings in the plugin settings and I am currently using it for other custom posts setup in the same way. I have tried deselecting and selecting the manual sort option and that did not make a difference.
I can also see the changes I make being saved in the correct relationship table but on the front end I do not see any changes.
Any assistance would be greatly appreciated.
Thanks!
-
This reply was modified 8 years, 3 months ago by
rkambasha.
Have you enabled the manual sort order on your custom post type?
how are you building your custom query?
Yes I have enabled the custom sort on the custom post type. I am using wp_query and passing the custom post type, category, posts_per_page as -1 and suppress filters set to false.
Here is the query:function work_items($categories)
{
$args = array(
‘post_type’ => ‘work_item’,
‘category_name’ => $categories,
‘numberposts’ => -1,
‘posts_per_page’ => -1,
‘suppress_filters’ => false
);$posts = new WP_Query($args);
return $posts;
}I call this function and then loop through the posts in a while loop and output the formatted html.
Can you post the sql query itself.
If this a development server can u disable the plugin and reactivate it again
-
This reply was modified 8 years, 4 months ago by
The topic ‘Apply Sort Order to custom query’ is closed to new replies.