admin管理员组文章数量:1422366
EDIT: I can't delete my question because of the open bounty, but it looks like the problem is caused by the "Post Types Order" plugin I have installed. I have it set to not order news
post types, but for some reason it's still adding its sorting to the main query. It's my mistake, you should always disable plugins when debugging issues.
I'm trying to set the default sorting on a column for my custom post type news
. The column sorts fine when the heading is clicked on, but when you first visit the post listing it will only sort by the default sorting (published date). Here's my pre_get_posts
action:
add_action( 'pre_get_posts', function( $query ) {
global $pagenow;
if ( !is_admin() || $pagenow !== 'edit.php' )
return;
$post_type = $query->get( 'post_type' );
$order_by = $query->get( 'orderby' );
if ( $post_type == 'news' ) {
if ( empty( $order_by ) )
$order_by = 'news-date';
switch ( $order_by ) {
case 'news-date':
$query->set( 'meta_key', 'date' );
$query->set( 'orderby', 'meta_value_num' );
break;
}
}
} );
After sorting manually by clicking the heading the URL becomes edit.php?post_type=news&orderby=news-date&order=asc
so I'm sure all my parameters match up.
I tried adding a var_dump( $query );
in the switch statement after setting the query and the query includes the updated meta_key
and orderby
values, so I'm assuming the query is being modified correctly, but still it's doing the default sorting.
I thought maybe the date
meta key was already used for some reason (maybe by a plugin) so I checked the wp_postmeta
table in the database but the only meta keys named date
were the values set with the ACF Date Picker, so everything is fine there.
Hopefully it's just something stupid I'm missing.
EDIT: I can't delete my question because of the open bounty, but it looks like the problem is caused by the "Post Types Order" plugin I have installed. I have it set to not order news
post types, but for some reason it's still adding its sorting to the main query. It's my mistake, you should always disable plugins when debugging issues.
I'm trying to set the default sorting on a column for my custom post type news
. The column sorts fine when the heading is clicked on, but when you first visit the post listing it will only sort by the default sorting (published date). Here's my pre_get_posts
action:
add_action( 'pre_get_posts', function( $query ) {
global $pagenow;
if ( !is_admin() || $pagenow !== 'edit.php' )
return;
$post_type = $query->get( 'post_type' );
$order_by = $query->get( 'orderby' );
if ( $post_type == 'news' ) {
if ( empty( $order_by ) )
$order_by = 'news-date';
switch ( $order_by ) {
case 'news-date':
$query->set( 'meta_key', 'date' );
$query->set( 'orderby', 'meta_value_num' );
break;
}
}
} );
After sorting manually by clicking the heading the URL becomes edit.php?post_type=news&orderby=news-date&order=asc
so I'm sure all my parameters match up.
I tried adding a var_dump( $query );
in the switch statement after setting the query and the query includes the updated meta_key
and orderby
values, so I'm assuming the query is being modified correctly, but still it's doing the default sorting.
I thought maybe the date
meta key was already used for some reason (maybe by a plugin) so I checked the wp_postmeta
table in the database but the only meta keys named date
were the values set with the ACF Date Picker, so everything is fine there.
Hopefully it's just something stupid I'm missing.
Share Improve this question edited Jun 27, 2019 at 17:53 Gavin asked Jun 25, 2019 at 16:08 GavinGavin 4347 silver badges21 bronze badges1 Answer
Reset to default 0 +50it looks like the problem is caused by the "Post Types Order" plugin I have installed
Yes, I believe so because your code actually worked for me, but after I installed and activated the plugin, your code no longer worked as expected.
I have it set to not order
news
post types, but for some reason it's still adding its sorting to the main query.
I don't know how you set it, but try one of these:
Set
ignore_custom_sort
(it's a customWP_Query
parameter) totrue
like so:switch ( $order_by ) { case 'news-date': $query->set( 'meta_key', 'date' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'ignore_custom_sort', true ); break; }
Or you can also use the
pto/posts_orderby/ignore
hook to disable the plugin's sorting:add_filter( 'pto/posts_orderby/ignore', function( $ignore, $order_by, $query ){ if ( 'lesson' === $query->get( 'post_type' ) ) { $ignore = true; } return $ignore; }, 10, 3 );
本文标签: custom post typesChanging default admin column sorting to an ACF Date Picker field
版权声明:本文标题:custom post types - Changing default admin column sorting to an ACF Date Picker field 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745364165a2655433.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论