情况就是这样,我有一个名为 Skill 的自定义税。我希望能够只显示技能设置为英语的日语。
我正在尝试学习如何使用 pre_get_posts 挂钩修改我的 get_posts 查询。这是我的例子,但是我遇到了错误:
注意:未定义的变量:postdata
这是我根据研究尝试过的:
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
// Fetch only posts tagged with "Japanese from English"
$taxquery = array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
);
$query->set( 'tax_query', $taxquery );
我确定上述查询有问题,我不完全理解。任何帮助,请解释如果可能的话阵列的每个字段。
答案 0 :(得分:5)
请尝试以下代码,这应该可行,
add_filter( 'pre_get_posts', 'wpshout_fundraiser_recent_posts' );
function wpshout_fundraiser_recent_posts( $query ) {
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'Your Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'Japanese from English',
'field' => 'skill',
'terms' => array( 'skill' ),
)
)
)
);
return $posts_array;
}
答案 1 :(得分:0)
您可以在这里查看:
$args = array(
'posts_per_page' => 5,
'post_type' => 'Post Type Name',
'tax_query' => array(
array(
'taxonomy' => 'category_taxonomy',
'field' => 'slug',
'terms' => "Category Name"
)));
$query = query_posts( $args );
while (have_posts()) : the_post();
the_content();
endwhile;
答案 2 :(得分:0)
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'post_type_name',
'tax_query' => array(
array(
'taxonomy' => 'taxonomy-name',
'field' => 'term_id',
'terms' => $cat->term_id,
)
)
)
);
答案 3 :(得分:0)
您可以尝试以下代码:
$custom_args=array(
'post_type' => "Your Post Type Name",
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> -1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'tax_query' => array(
array(
'taxonomy' => 'Your taxonomy slug',
'field' => 'slug',
'terms' =>"'Your Category Name"
)
),
'orderby' => 'id',
'order' => 'ASC'
);
$custom_my_query = null;
$custom_my_query = new WP_Query($custom_args);
$custom_my_total_count = count($custom_my_query);
if( $custom_my_query->have_posts() )
{
while ($custom_my_query->have_posts()) : $custom_my_query->the_post();
?>
<a href="<?php echo get_permalink();?>"><?php echo get_the_title($post->ID);?></a>
<?php
endwhile;
}
wp_reset_query($custom_my_query); // Restore global post data stomped by the_post().
}