admin管理员组文章数量:1429120
I have these two functions.
The difference is that one is filtering custom taxonomy (is_tax) and the other is filtering generic WordPress tags (is_tag).
The work as I'd like them to be it seems that the code is being duplicated.
This is Taxonomy:
add_action( 'pre_get_posts', 'sort_conference_by_date_tax' );
function sort_conference_by_date_tax( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
if ( $query->is_tax() ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
}
}
}
This is for Tags (Standard WordPress)
add_action( 'pre_get_posts', 'sort_conference_by_date_tag' );
function sort_conference_by_date_tag( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
if ( $query->is_tag() ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
}
}
}
I thought an array would work like this:
if ( $query->(array(('tag', 'tax'))) {
return 0;
}
But it didn't seem to work for me...
I guess I am only doing something that would save a fraction of a second by merging these functions so if it can't be done prob best to just leave it alone right?
Thanks
I have these two functions.
The difference is that one is filtering custom taxonomy (is_tax) and the other is filtering generic WordPress tags (is_tag).
The work as I'd like them to be it seems that the code is being duplicated.
This is Taxonomy:
add_action( 'pre_get_posts', 'sort_conference_by_date_tax' );
function sort_conference_by_date_tax( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
if ( $query->is_tax() ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
}
}
}
This is for Tags (Standard WordPress)
add_action( 'pre_get_posts', 'sort_conference_by_date_tag' );
function sort_conference_by_date_tag( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
if ( $query->is_tag() ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
}
}
}
I thought an array would work like this:
if ( $query->(array(('tag', 'tax'))) {
return 0;
}
But it didn't seem to work for me...
I guess I am only doing something that would save a fraction of a second by merging these functions so if it can't be done prob best to just leave it alone right?
Thanks
Share Improve this question edited May 13, 2019 at 0:52 Henry asked May 13, 2019 at 0:29 HenryHenry 9831 gold badge8 silver badges31 bronze badges 1- 1 This is essentially "How do I do an A or B check in PHP?" – Tom J Nowell ♦ Commented May 13, 2019 at 1:00
1 Answer
Reset to default 1Unless I'm missing something, I would think you could just use an OR (||) operator to check if it is_tag()
or is_tax()
:
add_action( 'pre_get_posts', 'sort_conference_by_date' );
function sort_conference_by_date( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
if ( $query->is_tag() || $query->is_tax() ) {
$query->set('orderby', 'meta_value');
$query->set('meta_key', 'start_date');
$query->set('order', 'DESC');
}
}
}
本文标签: taxonomyPossible to merge these two functions
版权声明:本文标题:taxonomy - Possible to merge these two functions? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745500422a2661004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论