I rewrote the code and now it works
The loop:
$args = array('post_type' => 'artist');
$wp_query = new WP_Query( array_merge( $args, array('nopaging' => true) ) );
The filter:
function theme_posts_orderby( $orderby, $query )
{
if ($query->query_vars['post_type'] == 'artist') {
// order artists by last name
$orderby = "RIGHT(post_title, LOCATE(' ', REVERSE(post_title)) - 1) ASC";
}
return $orderby;
}
add_filter('posts_orderby', 'theme_posts_orderby', 10, 2 );
Related Posts:
- Filter for “get_post_type_archive_link()”
- Multiple post types in archives (filter?)
- wp_query and comment_parent – select only posts with top level comments
- Want to filter only parent post in admin area
- is_main_query() not working for WP REST API
- Setting proper query for multiple custom admin filters
- Genesis filterable portfolio isotope [closed]
- creat filter with wp_query
- A method for ordering mixed dates in search result loop (theory only, no actual code)
- How can I made custom taxonomies relationship?
- Filter Posts from the Main Query
- Can I display custom post types in home.php or need page template?
- How do I add custom HTML to the content of an archive page’s posts?
- Only show posts with a specific term of an associated taxonomy in a custom post type archive
- Custom Post Type Archive Pagination
- Is possible register two archive pages for single custom post type?
- conditional filter
- WP Admin Dropdown List Filter for custom (ACF) field on custom post type(s)
- Filter posts by their related field’s custom field
- Tell wordpress to show a single page instead of an archive page
- How to use multiple orderby conditions on query search results? (orderby one post type, relevance)
- Output all terms slugs for a loop filter
- One of my headings is mysteriously coming up as a link in my CPT Archive
- how to pass args for archive.php query?
- WP Query + custom fields: How to query event posts from the current date backwards 6 months and organize it month by month?
- How to order posts of a custom post type by date DESC in dashboard Admin?
- Is there any way to get list of all possible filter hooks for all post types?
- Sort posts in loop by the WooCommerce Membership of the author
- wp_insert_post wrong post type [closed]
- Pagination doesn’t function properly for archive of a custom post type set as the front page
- How to get specific post meta by title or id
- Custom post type, custom taxonomy, query posts only from taxonomy (children of)
- How to properly rewrite pagination rules for a CPT to avoid 404 error on /page/2
- $post->post_meta not pulling any post in wordpress/php
- WP_Query of custom post type sorted by meta_key has unexpected results
- display posts of custom post type with custom taxonomy
- How to query for posts (hierarchical custom post type) that have children but are NOT top level?
- How do I show a link or ‘Read More’ button on a custom field excerpt when it is less than the word limit
- Create a WP_Query where if the first value of the first row is equal to the second compare other value
- How to filter url on post submission?
- Get the link of the first post of a custom taxonomy in a custom taxonomy list
- Pagination for search results of custom post type [duplicate]
- Custom Plugin – Query CPT – Display Results
- Sorting by meta_key different to search criteria?
- Shortcode / WP_Query in post changes context
- Modify a plugin function output from another plugin
- Custom Taxonomy Filter Issues
- All blog posts are not showing up
- How to fetch posts that are stored in the different table than (default) wp_posts?
- WP_Query order custom post type with certain meta key value by post modified date
- Cant’ Display Custom Post Type Title Base on Tax Terms
- Make Next and Previous on single-$posttype.php use the same order as archive-$posttype.php
- Custom post-type custom query – show all posts
- Event Archive by Month
- Display custom-post type based on the Title matching the current selected value
- Filter search posts by post meta?
- Modifying search results based on post_type
- Custom Post Type ‘Event’: Chronological list of recurring events from meta_values in array
- Pagination in custom post type archive.php not working
- How to achieve a multi-taxomony layout with posts?
- WP Query: If field X is empty, show posts based on field Y
- Sort custom post column by generated value?
- Store a value in global scope after init hook is fired
- How to search through all child taxonomies using WP_Query?
- Numeric pagination custom post type
- Custom meta fields not showing up in WP_Response Object via custom endpoint
- Query a Custom Post Type using SELECT that has ACF fields to compare dates
- How to get post by meta value
- Wrapping an unknown amount of posts inside separate HTML Containers during WP_Query loop
- Filter custom WP_Query by first letter of a custom field – hopefully using Search and Filter Pro?
- WP Query filter search. Result show all posts instead just custom post type
- Help Adding filter to Add Media button for custom post type
- Display posts of a hierarchical custom post type excluding parent posts
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- WordPress loop add heading before first of type
- get_post_meta for Custom Post Type ( CPT )
- Creating adminable dynamic filtering on custom post type
- Rewriting archive page slug to be different than custom post type slug
- WP_Query for CPT with filter by another WP_Query
- Is it possible to add query parameters on the archive page?
- Site loads very slow for archive custom post type page
- add current-menu-item to multiple custom post types
- How to query posts by meta keys AND under specific category?
- Shortcode display CPT Query only showing 1 post?
- Query stopped working
- How can I base a query off of the current page post type AND a meta value from another custom post type
- WP_Query and two custom fields returns no posts
- Fallback if statement based on the number filtered from it
- Custom Post-type not returning the right child_of
- How do I hide single category post on my post page
- Get images only from a certain post type
- Display ACF object field data using Elementor Custom Query
- WordPress custom field sorting, weird behavior: the latest post is at the end
- Default WordPress Query for a Specific Custom Post Type URL
- Sort ACF by custom taxonomy
- Dynamically count the number of custom post types associated to a custom taxonomy
- Add post id to url instead of WordPress default -2 suffix
- Custom Post Type posts not getting picked up in archive widgets
- Quering a post object based on another related post object
- How do I get an intermediary page between 2 custom post types?