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?
- Query by post title
- Get post with multiple meta keys and value
- Get posts for custom post type with WP_Query
- Custom sortable columns ordered by meta-value?
- Number of pages – multiple (custom) post types
- Enforcing canonical URLs with multiple custom post types
- WP_Query -> sort results by relevance (= most tags / taxonomy terms in common)
- Get latest 4 post on a custom post filtered by category
- WordPress custom post type category pagination 404 Error
- how to group custom post type posts by custom taxonomy terms
- Comparing timestamps in meta query doesn’t work
- Include both default and Custom Post Type in query modified inside pre_get_posts
- List all custom post type posts from a given category?
- How can I add a column in the wp_list_table of the admin area?
- Custom Post Type with static page for archive
- How to filter the description of a custom post type
- Filter home_url for custom post type
- Add Content Exclusively to a Custom Post Type Feed
- Loop on front-page.php
- Querying Term Posts in Loop
- Can’t get order_by meta_value_num to work properly
- orderby in custom WP Query does not work
- Unable to retrieve any posts of CPT in wp-admin
- Creating a navigation menu of all posts of a custom post type and their children posts?
- WordPress loop: Show only a Custom Post Type Taxononmy TERM
- How do you paginate a query grouped by month?
- Query Custom Post Type Taxonomy term with multiple parameters
- Custom filter function not working with Custom post type
- Archive for CPT
- pagination not working for category.php (custom post types in categories)
- Query based on custom fields start and end date
- Why is my WP Query not returning first result’s post meta?
- Different Limit number of post on different archive page
- WP_Query Custom Post Type if Category ID Equals
- Custom post types and ‘new WP_Query’
- Select2 AJAX and WP Query Returns ALL and does not filter
- WP_Query orderby not work with meta_key
- Having Issue on Ordering CPT by Custom Field In Custom WP Query
- Returning a custom content types with meta values
- List categories, subcategories and posts from custom taxonomy and custom post type
- Problem with Apply Filters on URL in meta box filed of custom port type
- Archive Page for Multiple Custom Post Types
- Searching post types
- Archive page for custom post type and custom taxonomy
- How to retrive Custom Post Type Meta Fields in Custom WP_Query
- wp_query not returning my custom post
- Loop for custom-post-type comparing taxonomy terms for “related” posts?
- Create a Custom Path to Archive
- Get next and prev item from custom WP_Query and Custom Post Type
- 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
- Query to show post current day
- Using ACF values in nested WP queries for CPT with date values in the past
- WP_Query: how to search tags in addition to a custom post type?
- Pagination Not Working When Used With WP_Query() `offset` Property
- Help understand and create a loop with WP_query
- How to show custom post count in archive page
- Do not show child pages within a file page
- WP_Query with all posts in one custom post type and only posts in another custom post type with a specific category
- Add content to wordpress edit.php page
- Search result based on URL
- filter rest api post by a acf filed
- How can I generate a list of post-type specific categories?
- Highlight a Post on archive page if it has a new comment?
- Custom post type with Filter and Page Nav
- New WP_Query not isolating custom post types on front-page template
- Keep same archive.php for post from archive?
- Diffrent search templates for different post types
- Pagination not working on homepage
- How to query custom post then display sections by meta value
- Using tax_query to get single post per category
- Show Posts in Vertical Tabs with Scrollbar
- How do I filter a custom post type loop by a field?
- A to Z list for custom post types
- How to manage wordpress knowledge base/wiki/posts collections
- Assign a tag to custom post type using a query