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?
- Create an archive page by combining two post type
- author archives, showing all custom post types, problem on pagination
- Combining sorted and random CPT
- How to add post_type=value when editing that post type in the WordPress admin?
- Custom template page with custom archives listing by user
- Why is conditionally loading a custom plugin’s code only on a specific custom post type causing the site content to disappear?
- Display CPT posts based on specific taxonomy
- CPT Archive with core Category
- Can’t query tag and post_type at the same time
- How to query custom post types posts filtered by multiple custom taxonomies through a form selection
- shortcode to show custom post types by category
- Unable to retrieve any posts of CPT in wp-admin
- How can i change the output of posts_nav_link();
- WP_Query: include custom post type only with specific meta value
- Pagination on a custom post type loop
- Changing number of posts per page on CPT-archive.php, have tried 20-30 code chunks so far
- showing custom post types of a certain category only
- WP_Query orderby modified to include custom meta changes
- WordPress custom loop filter by meta_key and value with serialize data
- Returning a list of custom post types excluding those without a specific meta_value
- query private custom post type
- Display custom post type from dynamic custom field
- Query/list all terms and their custom post count
- Append date to custom post type url slug
- Linking to the most recent post in a Custom Post Type
- Custom Post Type Archive Page Filtering
- Root slug of taxonomy returning 404
- How to make sure content doesn’t display if selection is empty
- How to modify archive query with pre_get_posts to append CPTs?
- Filtering WP_Query Dynamically on the Front-End
- get the custom post type title/name
- Show one item per category of a custom post type
- Why get_posts() not returning only selected category posts from Custom Post Type?
- Storing posts from query and accessing later via AJAX call
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Get archive post type name
- Query custom post type with tags
- wp_query check if integer exists in custom field’s array
- Tag Archive for Custom-Post-Type Posts yielding 404 when permalinks set to postname
- convert custom query to wp_query
- Wrong request query on cpt and tax
- How can I see a list of my Custom Post Types of the last term I was in?
- Previous/Next Link by Meta Value in CPT
- Show custom post type filtered by category
- Randomize Posts. Skip the first post in ascending order
- Creating a navigation menu of all posts of a custom post type and their children posts?
- Get custom taxonomies from multiple posts
- URL of a custom post type’s post format archive?
- Pagination for Custom Taxonomy Page [duplicate]
- Search / Filter posts on Title/Content OR Tags
- Pagination on with query_posts in custom post type template
- Wp_query: sort by PHP variable
- WP_Query most viewed posts, in multiple Post Types, last 30 days, excluding a specific taxonomy term
- Custom-Posttype & Custom Taxonomy WP_Query
- Crafting WP_Query array, sort by date
- Custom loop with multiple taxonomy queries
- Return one unique custom post type result when it shares a custom taxonomy in WP_QUERY?
- Displaying custom taxonomy in the admin list of a custom post type
- Custom Post Type with archive page but no detail page
- Converting the_content string to an array?
- Create template for taxonomy results limited by Custom Post Type
- forming WP_Query for posts of all post types but from specific categories
- How can I run this code once so that my generated post title doesn’t keep changing on publish/update?
- Get latest 3 posts from multiple CPT in one query
- Loop to fetch 3 post_thumbnail instances from 3 most recent custom post types named “portfolio”
- filter custom post type by meta key in dashboard
- Display upcoming Events for next 7 Days
- Categories of custom taxonomy don’t show any posts
- Custom post type – get_day_link()
- Isotope Filtering with Bootstrap Tabs – Custom Post Type Query Loop in each Tab (Have to click twice to filter)
- Custom post ui plugin & WP_Query – Polylang
- How to add a post slug to a url?
- Change CPT archive title
- How can I get the number of custom post type posts that have a specific attachment image set?