After making Custom Sortable Columns everything was awesome, right?
Well… Mostly.
When I tried to sort a search, I got this:
WordPress database error: [Not unique table/alias: 'wp_postmeta'] SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND (((wp_posts.post_title LIKE '%kate%') OR ( (wp_postmeta.meta_key IN ( 'lezchars_actor', 'lezshows_worthit_details', 'lezshows_plots', 'lezshows_episodes', 'lezshows_realness_details', 'lezshows_quality_details', 'lezshows_screentime_details' ) ) AND (wp_postmeta.meta_value LIKE '%kate%') ) OR (wp_posts.post_excerpt LIKE '%kate%') OR (wp_posts.post_content LIKE '%kate%'))) AND ( wp_postmeta.meta_key = 'lezchars_type' ) AND wp_posts.post_type = 'post_type_characters' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 20
Not good.
And not actually (really) related to the sortability! No no, I did this to myself by adding in optimized post-meta search. You see, that runs on the front and the back end of WordPress.
I knew it had to be the search tweaks I’d made when I saw this:
OR ( (wp_postmeta.meta_key IN ( 'lezchars_actor', 'lezshows_worthit_details', 'lezshows_plots', 'lezshows_episodes', 'lezshows_realness_details', 'lezshows_quality_details', 'lezshows_screentime_details' ) )
Right away I realized it had to be my custom queries both calling from wp_postmeta
and, in doing so, stemming all over each other. Not good at all.
The fix was relatively simple. All I had to do was move the filters to run only if we’re not in wp-admin.
/** * Only run if we're NOT in the admin screen! */ if ( ! is_admin() ) { add_filter( 'posts_join', 'lezwatch_search_join' ); add_filter( 'posts_where', 'lezwatch_search_where' ); add_filter( 'posts_distinct', 'lezwatch_search_distinct' ); }
This meant that the internal post search wouldn’t query those fields, but in the grand scheme of things, that’s okay. Generally when I’m looking for a post in the search, I’m looking for it by title. If I wanted to be more particular, I could have it not use my custom joins when I’m on a post listing page, but allow it if I’m editing a post (so adding a new link would search all that content too).
For now, this is what I need.