Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

WordPress request fiter order by related post’s post_title

After a short sleepless night with this running through my head, I have a solution that works. I’m not sure if it’s the best solution but for those that may have a similar problem, here’s what I did.

First, I abandoned trying to use the WordPress request filter for this particular ordering. Instead, I used the WP posts_clauses filter as follows:

add_filter( 'posts_clauses', 'my_posts_clauses_filter', 20, 1 );
function my_posts_clauses_filter( $pieces )
{
    global $wpdb;
    if ( isset( $_GET[ 'post_type' ] )
     and isset( $_GET[ 'orderby' ] )
     and 'person' == $_GET[ 'post_type' ]
     and 'person_school' == $_GET[ 'orderby' ]
     and ( 'ASC' == $_GET[ 'order' ] or 'DESC' == $_GET[ 'order' ] )
    )
    {
        $pieces[ 'join' ] .= " INNER JOIN {$wpdb->postmeta} my_postmeta1 ON ( {$wpdb->posts}.ID = my_postmeta1.post_id AND my_postmeta1.meta_key = 'person-school-id' )";
        $pieces[ 'join' ] .= " INNER JOIN {$wpdb->posts} my_posts1 ON ( my_posts1.ID = my_postmeta1.meta_value )";
        $pieces[ 'orderby' ] = 'my_posts1.post_title ' . $_GET[ 'order' ];
    }
    return $pieces;
}

That’s it.

Once note of caution. This could have unintended consequences. I had another routine that used WP_Query to return a list of all schools for filtering. After adding the above posts_clauses filter, my get all schools routine was not returning anything when the $_GET entries were set. I got around this by surrounding the WP_Query request with a remove_filter and add_filter for the above posts_clauses filter.

Related Posts:

  1. Query all posts where a meta key does not exist
  2. Meta query with boolean true/false value
  3. Get post with multiple meta keys and value
  4. Enforcing canonical URLs with multiple custom post types
  5. How Do I Use WP_Query to Run This Database Query as Search Result?
  6. Can’t sort order of wp_query with 2 meta keys
  7. Query for posts from any post type but only add instock products
  8. WordPress Orderby Numeric Value Not Working
  9. orderby in custom WP Query does not work
  10. Display custom post type from dynamic custom field
  11. Displaying a div from an assigned meta_value when on a page
  12. How to get specific post meta by title or id
  13. $post->post_meta not pulling any post in wordpress/php
  14. WP_Query of custom post type sorted by meta_key has unexpected results
  15. Returning a custom content types with meta values
  16. WP_Meta_Query object with conditionals
  17. How to retrive Custom Post Type Meta Fields in Custom WP_Query
  18. After inserting new post with wp_insert_post() the post is not visble to WP_Query, but the same WP_Query works for post inserted from wp-admin panel
  19. Use WP_query to match post types based on custom field values
  20. How to sort by multiple values in a nested WP_Query
  21. Custom meta fields not showing up in WP_Response Object via custom endpoint
  22. How to get post by meta value
  23. How do I get all authors posts of a custom post type outside loop
  24. Linking posts together with Advanced Custom Fields “both ways”
  25. How to get Current week and current date record wp query
  26. How to get post meta for custom post type and taxonomy
  27. get_post_meta for Custom Post Type ( CPT )
  28. WordPress custom post type
  29. Custom Post Type meta oembed html output resulting in WSoD
  30. Values inside a custom field to determine which category posts to display
  31. Querying meta values within an array
  32. Quering a post object based on another related post object
  33. Wp_query: sort by PHP variable
  34. WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
  35. Custom-Posttype & Custom Taxonomy WP_Query
  36. Crafting WP_Query array, sort by date
  37. Custom loop with multiple taxonomy queries
  38. Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
  39. How to implement a Google map store locator
  40. Converting the_content string to an array?
  41. forming WP_Query for posts of all post types but from specific categories
  42. Get latest 3 posts from multiple CPT in one query
  43. Deleting Custom Posts & Meta Data on Uninstall
  44. Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
  45. Widget: Custom Post Type Post Listing Dropdown on Admin Side
  46. Display upcoming Events for next 7 Days
  47. Post image in WordPress not appearing on home page
  48. How do I create an archive page as a including metadata?
  49. Isotope Filtering with Bootstrap Tabs – Custom Post Type Query Loop in each Tab (Have to click twice to filter)
  50. Custom post ui plugin & WP_Query – Polylang
  51. changing meta value and meta key of price field
  52. How can I get the number of custom post type posts that have a specific attachment image set?
  53. Posting to a Custom Post Type from front end – user generated content
  54. How to use load more custom post type data normal or according to on click category tab In WP
  55. orderby rand is not working for a custom post types
  56. Update Post Meta for a logged in user
  57. Getting meta in editor plugin, and event triggering issue
  58. Problem querying Custom post type by custom fields
  59. Orderby CPT custom fields not working
  60. Custom taxonomy rewrite give pagination 404
  61. How to duplicate entire custom post type
  62. WP_Query order by custom field, then randomly order some of results
  63. A better way to add a meta box to custom post types
  64. Restrict Access to Posts based on Custom User and Post Meta Data
  65. WP_Query inside foreach loop returning same value for all options when filtered using ajax
  66. Mixing 2 custom post types with posts output in specific pattern
  67. Custom post taxonomies as tax_query terms?
  68. Two Custom Post Types Many to Many Relationship
  69. get_post_meta not working on publishing
  70. Should wp_postmeta meta_key always be unique for a given post_id?
  71. creat filter with wp_query
  72. Troubles with acf/save_post and WP_Query
  73. A method for ordering mixed dates in search result loop (theory only, no actual code)
  74. Query custom post type with ACF Date
  75. How to do WP_Query with two meta fields with orderby clause
  76. Stuck in Order by more then one
  77. Display featured image from one CPT within another CPT query
  78. how can I register a post_meta field in an existing CPT and then call it again with get_post_custom()?
  79. get_post_meta returning no value in custom post type alert email
  80. Custom WP Query on custom meta and sort by multiple meta keys value
  81. Show only one post on custom post type archive
  82. Woocommerce search pagination not working
  83. How to order WP_Query by parent for hierarchical Custom Post Type?
  84. Including metaboxes from custom post types in global search — continued
  85. WordPress loop: Show only a Custom Post Type Taxononmy TERM
  86. post meta parameter in post custom-post-type endpoint with restapi
  87. How can I made custom taxonomies relationship?
  88. How do I set all of a particular post meta to a value within the custom post type I’m in?
  89. Query Multiple Custom Posts by Custom Fields
  90. What’s the WP way to load remaining custom posts?
  91. Why is this query not working? (Standard posts + custom post type)
  92. How do you paginate a query grouped by month?
  93. Custom taxonomies relationship
  94. Get Posts by multiple custom fields is not working
  95. Custom post type ‘articles’ ignores posts_per_page, reserved post_type?
  96. Several post types on WP Query by tag and taxonomy
  97. Update postmeta Parent when post_status child change
  98. WP Query with categories only shows one post and ignores the category
  99. Metabox not show in categories custom post type cmb2
  100. Polylang non-default language ignores tags in WP_Query
Categories custom-post-types Tags custom-post-types, post-meta, request-filter, wp-query
How do I set up a local version of my https wordpress site for testing and development using MAMP
wp_insert_term created and set for first once only

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress