我有一个名为事件的自定义帖子类型的存档页面,我使用自定义字段通过下拉菜单对其进行过滤,例如,在下拉菜单中选择位置',当您点击伦敦它将显示位于'伦敦的帖子。等等。这是我使用的代码
// allow the url to alter the query
// eg: http://www.website.com/events?location=melbourne
// eg: http://www.website.com/events?location=sydney
if( !empty($_GET['location']) )
{
$location = explode(',', $_GET['location']);
//Add our meta query to the original meta queries
$meta_query[] = array(
'key' => 'location',
'value' => $location,
'compare' => 'IN',
);
}
我已将粘贴帖子添加到循环中,并且它们在事件的默认存档页面上显示为粘滞,但即使过滤后的选项不适用于粘贴帖子,它们也会显示在已过滤的页面上。因此,当我选择'曼彻斯特'时,粘性帖子即使设置为伦敦也会显示。我想知道如何正确地过滤粘贴帖子以及正常帖子,但是当过滤器选项适用于它们时,仍然可以保持粘性。我使用了Sticky Custom Post Types插件,并在我的functions.php中添加了以下代码,以便在存档页面上显示粘性帖子。对此的任何建议都将是一个绝对的救命!
function wpb_cpt_sticky_at_top( $posts ) {
// apply it on the archives only
if ( is_main_query() && is_post_type_archive() ) {
global $wp_query;
$sticky_posts = get_option( 'sticky_posts' );
$num_posts = count( $posts );
$sticky_offset = 0;
// Find the sticky posts
for ($i = 0; $i < $num_posts; $i++) {
// Put sticky posts at the top of the posts array
if ( in_array( $posts[$i]->ID, $sticky_posts ) ) {
$sticky_post = $posts[$i];
// Remove sticky from current position
array_splice( $posts, $i, 1 );
// Move to front, after other stickies
array_splice( $posts, $sticky_offset, 0, array($sticky_post) );
$sticky_offset++;
// Remove post from sticky posts array
$offset = array_search($sticky_post->ID, $sticky_posts);
unset( $sticky_posts[$offset] );
}
}
// Look for more sticky posts if needed
if ( !empty( $sticky_posts) ) {
$stickies = get_posts( array(
'post__in' => $sticky_posts,
'post_type' => $wp_query->query_vars['post_type'],
'post_status' => 'publish',
'nopaging' => true
) );
foreach ( $stickies as $sticky_post ) {
array_splice( $posts, $sticky_offset, 1, array( $sticky_post ) );
$sticky_offset++;
}
}
}
return $posts;
}
add_filter( 'the_posts', 'wpb_cpt_sticky_at_top' );
// Add sticky class in article title to style sticky posts differently
function cpt_sticky_class($classes) {
if ( is_sticky() ) :
$classes[] = 'sticky';
return $classes;
endif;
return $classes;
}
add_filter('post_class', 'cpt_sticky_class');