This is untested, but I think it should work. Add the filter in pre_get_posts
if it’s your post type archive, otherwise remove it for any queries that follow the post type archive main query.
function wpd_add_posts_orderby( $query ){
if( $query->is_post_type_archive('your-cpt') ){
add_filter('posts_orderby', 'edit_posts_orderby');
} else {
remove_filter('posts_orderby', 'edit_posts_orderby');
}
}
add_action( 'pre_get_posts', 'wpd_add_posts_orderby' );
Related Posts:
- pre get posts changing the query
- How do I query a custom post type with a custom taxonomy?
- Get the ID of the latest post
- Return all custom meta data for one custom post type
- Display Custom Post Type Fields
- order by meta_value serialized array
- Meta Query “IN” doesn’t work with ACF checkbox filter
- How to get the parent’s taxonomy?
- How Do I Use WP_Query to Run This Database Query as Search Result?
- Loading post content in FancyBox
- Limiting number of custom posts shown on taxonomy page
- Include custom post type in “all posts”
- Multiple templates for custom post type
- ascending order custom post type
- How to get only one category of custom post type?
- Used with meta_query in query_posts works slowly
- How to make an archive page displaying posts in a date range
- How to display recent posts added in several custom post types
- Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query
- Using pre_get_posts to Filter Posts
- How to dynamically build a multiple taxonomy query loop within a post type’s single loop?
- List custom post type by custom tax term as page title
- Custom query shows custom post types in trash
- Multiple post type queries (with specific arguments for each)
- Custom search for custom post meta with pre_get_posts interferes with WP search
- How to sort custom post type posts in default order by multiple fields?
- Change archive page template using pre_get_post
- Query/list all terms and their custom post count
- How to modify archive query with pre_get_posts to append CPTs?
- Advanced or not so advanced pre_get_posts query
- How to query_post custom posts within default taxonomy?
- How to use custom taxonomies to reference complex relationships?
- Query Custom posts of same taxonomy as the post itself
- Showing posts from different categories and from custom post type
- get custom post type
- Polylang non-default language ignores tags in WP_Query
- How to edit this code to get the categories in achieve page?
- Category with post type pagination returns 404
- Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
- Custom Post Type Archive Error, Takes Last Post as Title and Doesn’t Work
- Custom post type archive page for multiple post types
- Listing all term items alphabetically / sorting loop
- How to display Related Posts based on number of taxonomy terms matched
- How to show only the most recent post on my custom post type archive?
- How to make WP_Query not to show irrelevant posts?
- What is the ID parameter for custom post types in query_posts?
- How to limit post per page of custom post type?
- Excluding a Custom Post Type with a specific tag using pre_get_posts
- Querying multiple values from a single key
- Pagination not working on custom query on a page
- Prevent A Specific Custom Post Type Showing In WP Search Results Page
- Can’t change posts per page in WordPress Post Type Query
- Best way to create a search for custom post type by custom field values
- List all posts in taxonomy term
- Query all post types but limit to parents
- HM CMB: Post Select Field for CPT ID
- pre_get_posts works in post type archive but not in single post
- 4 posts per page from single category
- Display Posts Query with IF function
- Query Multiple Custom Post Types & Exclude a Taxonomy Term
- Getting custom post type info using get_adjacent_post
- Custom Post Type Query W/Category Dropdown
- Query all posts under one taxonomy? Why is my code not working?
- Find Posts based on Child Post value
- Showing only posts from the current user who is logged in?
- Why can’t I query more than 1 post type at a time?
- Order posts by taxonomy terms
- Limit custom post type to the authors only on front-end!
- How to hide private posts even if user is admin
- Order Posts in Custom Order
- List all posts associated under custom taxonomy
- Getting Custom Posts with Custom Taxonomy
- Query the title of the page to show posts with matching category in the loop
- CPT year wise archive based on custom date Field
- What’s the most efficient way to get two queries based on an if statement?
- How to Create movie schedule by custom post type query
- How do I get the posts from single page custom post type from a sidebar to display in a page?
- Main site single-property.php design, as homepage of a multisite
- pre_get_posts with multiple post types AND a meta_key
- WordPress custom post type
- How to get main div html to print a list of posts inside?
- WordPress custom post type queries
- Fetch post meta in the same query as the main loop
- Problem to show custom post type in archive page category wise
- Adding custom post types to the default loop, yet only posts that has terms from the core taxonomies
- Query Distinct Taxonomies of Custom Post Type
- Custom Query With Multiple Meta Key Value
- Query Posts From Multiple Post Types
- custom post types, pre_get_posts, wp_list_categories
- Advanced Custom Fields: query posts filtered by multiple field values
- How to apply order on custom taxonomy and custom meta key on custom post type
- Inserting two categories
- How to exclude certain portfolios from a loop
- show custom post’s post in two different divs [duplicate]
- How to get Custom post type by related category ID
- Display ACF object field data using Elementor Custom Query
- pre_get_posts causes Custom Posts appear under ‘Pages’ menu
- Assign a tag to custom post type using a query
- Sorting by Title for Post Archive Categories for Custom Post Type
- How can I incldue a “private” post type in a loop for public users?