admin管理员组文章数量:1023009
Is it possible to filter events (WP Query) within a Custom Taxonomy archive by ‘Start Dates’?
I create a Custom Post Type called “Events” and in that “Events” is a Start Date
When I view Custom Taxonomy listing data from the Custom Post Type I’d like for the events to be filtered by the Start Date in a DES manner.
I thought this code would work - am I on the right track?
//Using Wordpress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
if (($query->is_main_query()) && (is_tax('country')))
$query->set( 'post_type', 'your-post-type' ); // not sure??
$query->set( 'posts_per_page', '-1' );
$query->set( 'meta_key', 'start_date' ); // this is meta_field
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );
The above comes from here: Custom Taxonomy order by Custom Field
I thought the above would work for me but it didn't - perhaps because I wasn't sure what to add in the $query->set( 'post_type', 'your-post-type' ); bit
What should I be adding there?
Is it possible to filter events (WP Query) within a Custom Taxonomy archive by ‘Start Dates’?
I create a Custom Post Type called “Events” and in that “Events” is a Start Date
When I view Custom Taxonomy listing data from the Custom Post Type I’d like for the events to be filtered by the Start Date in a DES manner.
I thought this code would work - am I on the right track?
//Using Wordpress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
if (($query->is_main_query()) && (is_tax('country')))
$query->set( 'post_type', 'your-post-type' ); // not sure??
$query->set( 'posts_per_page', '-1' );
$query->set( 'meta_key', 'start_date' ); // this is meta_field
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );
The above comes from here: Custom Taxonomy order by Custom Field
I thought the above would work for me but it didn't - perhaps because I wasn't sure what to add in the $query->set( 'post_type', 'your-post-type' ); bit
What should I be adding there?
Share Improve this question asked May 11, 2019 at 6:26 HenryHenry 9831 gold badge8 silver badges31 bronze badges 1 |1 Answer
Reset to default 0Nothing. You shouldn't need to set the post type. Your taxonomy archive is presumably already querying the correct post type.
function customize_customtaxonomy_archive_display ( $query ) {
if ( $query->is_tax( 'country' ) ) {
$query->set( 'meta_key', 'start_date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
}
Note:
$query->is_main_query()
is redundant when checking$query->is_tax()
, because (as far as I'm aware) secondary queries can never be a taxonomy archive.- Setting the posts per page has nothing to do with sorting, so I've omitted it.
- You need to use
{}
withif
statements, otherwise the condition will only apply to the next line. The original code you've copied will apply that sorting and posts per page value to all queries. Even if you only have one line, omitting the brackets is considered poor practice.
Is it possible to filter events (WP Query) within a Custom Taxonomy archive by ‘Start Dates’?
I create a Custom Post Type called “Events” and in that “Events” is a Start Date
When I view Custom Taxonomy listing data from the Custom Post Type I’d like for the events to be filtered by the Start Date in a DES manner.
I thought this code would work - am I on the right track?
//Using Wordpress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
if (($query->is_main_query()) && (is_tax('country')))
$query->set( 'post_type', 'your-post-type' ); // not sure??
$query->set( 'posts_per_page', '-1' );
$query->set( 'meta_key', 'start_date' ); // this is meta_field
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );
The above comes from here: Custom Taxonomy order by Custom Field
I thought the above would work for me but it didn't - perhaps because I wasn't sure what to add in the $query->set( 'post_type', 'your-post-type' ); bit
What should I be adding there?
Is it possible to filter events (WP Query) within a Custom Taxonomy archive by ‘Start Dates’?
I create a Custom Post Type called “Events” and in that “Events” is a Start Date
When I view Custom Taxonomy listing data from the Custom Post Type I’d like for the events to be filtered by the Start Date in a DES manner.
I thought this code would work - am I on the right track?
//Using Wordpress Pre-Get filter to order the custom taxonomy by custom field
function customize_customtaxonomy_archive_display ( $query ) {
if (($query->is_main_query()) && (is_tax('country')))
$query->set( 'post_type', 'your-post-type' ); // not sure??
$query->set( 'posts_per_page', '-1' );
$query->set( 'meta_key', 'start_date' ); // this is meta_field
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
add_action( 'pre_get_posts', 'customize_customtaxonomy_archive_display' );
The above comes from here: Custom Taxonomy order by Custom Field
I thought the above would work for me but it didn't - perhaps because I wasn't sure what to add in the $query->set( 'post_type', 'your-post-type' ); bit
What should I be adding there?
Share Improve this question asked May 11, 2019 at 6:26 HenryHenry 9831 gold badge8 silver badges31 bronze badges 1-
'your-post-type'
will be your "Events" type slug, probably'event'
. – nmr Commented May 11, 2019 at 6:53
1 Answer
Reset to default 0Nothing. You shouldn't need to set the post type. Your taxonomy archive is presumably already querying the correct post type.
function customize_customtaxonomy_archive_display ( $query ) {
if ( $query->is_tax( 'country' ) ) {
$query->set( 'meta_key', 'start_date' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'ASC' );
}
}
Note:
$query->is_main_query()
is redundant when checking$query->is_tax()
, because (as far as I'm aware) secondary queries can never be a taxonomy archive.- Setting the posts per page has nothing to do with sorting, so I've omitted it.
- You need to use
{}
withif
statements, otherwise the condition will only apply to the next line. The original code you've copied will apply that sorting and posts per page value to all queries. Even if you only have one line, omitting the brackets is considered poor practice.
本文标签: wp queryWPQuery and DES sort for Custom Taxonomy based upon a meta field
版权声明:本文标题:wp query - WP_Query and DES sort for Custom Taxonomy based upon a meta field? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745503268a2153487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'your-post-type'
will be your "Events" type slug, probably'event'
. – nmr Commented May 11, 2019 at 6:53