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?
- what is the correct way to compare dates in a WP query_posts meta_query
- How to get post content by calling ajax?
- How to show all posts of the category in wordpress?
- Get the ID of the latest post
- Using meta_query, how can i filter by a custom field and order by another one?
- Return all custom meta data for one custom post type
- Display a query with multiple post types and same relationship on a single page
- Prevent pre_get_posts filter on specific post type
- Fail to compare dates in meta_query
- How to filter custom posts by tags and custom fields?
- Order by & include array by specific post ids
- How to call a custom post with get_posts() instead of query_posts()?
- Custom post type archive 404’s with paginate_links
- $query->set in pre_get_posts is unintentionally affecting the backend
- How to put custom post types on front page
- Add Custom Post Type to Current Query
- How to show more posts on an archive page?
- Include post id[s] into WP_Query()
- Pre get posts for single post
- Display Custom Post Type Fields
- Querying custom posts and regular posts
- How to get my Custom Post Type to display in Recent Posts using “pre_get_posts”
- Loading Custom Post Type Events into jQuery-based FullCalendar?
- Limit the post for differents custom post type in the same wp_query
- Retrieving 3 latest post from each of 5 different custom post types
- Pre_get_posts Gives 404 on Custom Post Type
- How to hook get_terms() to only show count of posts that have custom meta
- Using Offset in Custom Post Type Query
- filter search result with custom post type meta key
- how to group custom post type posts by custom taxonomy terms
- Order posts by (hierarchical custom) taxonomy terms and term children
- order by meta_value serialized array
- Posts per Page on custom Taxonomy Template
- Meta Query “IN” doesn’t work with ACF checkbox filter
- Using Query Posts With Multiple Post Types And A Taxonomy
- 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
- Query posts by custom post type and custom taxonomy
- Limiting number of custom posts shown on taxonomy page
- Include custom post type in “all posts”
- Multiple templates for custom post type
- Query post types with multiple keys
- Can’t sort order of wp_query with 2 meta keys
- ascending order custom post type
- posts_nav_link on single post template
- Sort custom post types by last name in the backend
- Query not returning CPT posts
- Displaying custom post type on category pages but not on blog listings
- Post Rank on Single Post page based on custom field
- Custom post type with tags
- How to get only one category of custom post type?
- Used with meta_query in query_posts works slowly
- Unable to display multiple post types in same query (WPML WP_Query)
- Query for posts in 2 taxonomies
- Prevent change with new query_posts()
- How to make an archive page displaying posts in a date range
- Get permalink and title by post name?
- How to get current post id of a custom post type in a loop using template singel-{custom type}.php?
- Custom Post Type sorted by Title
- Get all posts WHERE custom_field is LIKE value
- Quickest way to get last or oldest post date – WP Query
- How to display recent posts added in several custom post types
- Display all Posts and only certain Custom Post Types with meta fields
- Building an Advanced Search (text, tags, category, custom fields) – Getting the wrong SQL query
- Using pre_get_posts to Filter Posts
- ‘category__and’ for custom taxonomy?
- Post the content of a specific “Custom Post Type” post within a post using a shortcode
- Conditionally Query Custom Post Types by Post Meta for Blog Home Page?
- Display custom post types with custom date field value (before today) & order by custom date field
- Separate Posts and Custom Post Type in Custom Taxonomy archive template
- reference the current category being used in the category.php page
- 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
- query_posts with a custom post type, a meta_query and sorting by post date?
- 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
- Issue with pre_get_posts on custom post type archive pages
- Using pre_get_posts to filter one loop in a multiloop archive
- Combine query_posts() and get_posts() into single query
- How to sort custom post type posts in default order by multiple fields?
- how can i get posts from custom post type particular taxonomy category
- Setting posts per page in query_posts
- How to insert content from another Custom Post type into Post?
- Custom post type show on both Home and Date Archive
- Pagination is not working on single-{slug}.php but works fine on page-{slug}.php
- Sort by custom field is not working
- pre_get_posts on custom post type
- Custom query works but returns “Undefined Offset: 0”
- Sorting multiple custom post types without a meta key/value pair by sort order
- Custom post types instead of regular post’s categories in a page template
- Change or update WordPress loop based on dropdown selection
- Change archive page template using pre_get_post
- How to query custom post types posts filtered by multiple custom taxonomies through a form selection
- What’s a better alternative to this code?
- Display custom post type from dynamic custom field
- Query/list all terms and their custom post count