并非所有帖子都显示在wordpress过滤器中

时间:2018-10-04 15:29:32

标签: wordpress

我有一个带有过滤器的页面,该过滤器按类别显示帖子。它成功过滤。 这是选择代码:

<select class="selectBox" id="division" name="division">
  <option value="0">Topic</option>
  <option value="Compliance">Compliance</option>
  <option value="Intelligence">Intelligence</option>
  <option value="Investigations">Investigations</option>
</select>

如果单击主题选项,然后单击提交按钮,它将再次返回所有帖子,但停止在10个帖子上。

这是functions.php文件中的过滤器代码

function ajax_filter_function(){
$args = array(
    'orderby' => 'date', // we will sort posts by date
    'order' => 'DESC',
    'post_type'=>'post',
    'post_status' => 'publish'

);

// for categories
if(!empty( $_POST['division'] ) && !empty($_POST['type']) ) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'post_status' => 'publish',
            'terms' => array(strtolower(str_replace(' ','-',$_POST['type'])))
        ),
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'post_status' => 'publish',
            'terms' => array(strtolower(str_replace(' ','-',$_POST['division'])))
        )
    );
}elseif(!empty( $_POST['division'] )) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'post_status' => 'publish',
            'terms' => array(strtolower(str_replace(' ','-',$_POST['division'])))
        )
    );
}elseif(!empty( $_POST['type'] )) {
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'post_status' => 'publish',
            'terms' => array(strtolower(str_replace(' ','-',$_POST['type'])))
        )
    );
}else{
    $args['tax_query'] = array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'post_status' => 'publish',
            'terms' => array('resources')
        )
    );
}
$query = new WP_Query( $args );

if( $query->have_posts() ) :
    while( $query->have_posts() ): $query->the_post();
        $post_cat = get_post_meta(get_the_ID(), 'primary_category', true);
        echo'<li style="background:url('.get_the_post_thumbnail_url($query->ID,'full').') no-repeat;background-size:cover;background-position:center;">
            <a href="'.get_the_permalink().'">
                <div class="cat-bar '.strtolower(str_replace(' ','-',$post_cat)).'">'.ucwords($post_cat).'</div>';
                $post_cat = get_post_meta(get_the_ID(), 'primary_category', true);
                foreach ( (get_the_category() ) as $child ) {
                    if ( $child->cat_name != $post_cat && $child->cat_name != 'Uncategorized' && $child->cat_name != 'Thank You Page'){
                        echo'<div class="cat-bar sec-cat filtered-cat">'.$child->cat_name.'</div>';
                    }
                }
        echo '<div class="title-section"><div class="title">'.get_the_title().'</div>';
        if($post_cat == 'Event') { 
            echo'<div class="date"><img src="'.get_template_directory_uri().'/img/new/clock.png" /> '.get_field('event_date', get_the_ID()).'</div>';
        }else{ 
            echo'<div class="date"><img src="'.get_template_directory_uri().'/img/new/clock.png" /> '.get_the_time('F jS, Y').'</div>'; 
        }
        echo'
                </div>      
            </a>
            </li>
        ';
    endwhile;
    wp_reset_postdata();
else :
    echo '<p style="width:100%;text-align:center;">Sorry, no resources match that criteria. <a href="/resources">Remove filters</a> and try again.</p>';
endif;

die();

}

add_action('wp_ajax_myfilter','ajax_filter_function'); add_action('wp_ajax_nopriv_myfilter','ajax_filter_function');

this是它所存在的站点,以供参考。 您会发现,如果单击过滤器,则会删除最后两篇文章。

帮助!

1 个答案:

答案 0 :(得分:2)

如果您在查询中未提供posts_per_page参数,它将默认为“设置”>“读数”中设置的值,通常为10。

posts_per_page参数添加到查询参数中,值为-1,以显示所有帖子,或根据需要调整值:

$args = array(
    'orderby' => 'date', // we will sort posts by date
    'order' => 'DESC',
    'post_type'=>'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);