在自定义WP查询中创建帖子过滤器

时间:2018-11-15 10:12:51

标签: php wordpress

我正在尝试为WordPress上的帖子供稿创建过滤器栏。

我正在尝试使用$_GET方法来实现这一点,在该方法中,我根据URL中存在的参数更改了Wp-query。

由于某种原因,它不适用于所有情况;请注意,参数为

  1. 年份
  2. 类别
  3. 关键字

这是我的wp查询代码:

$current_url = $_SERVER['REQUEST_URI'];


    $count = 0; $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;


    if ( !isset($_GET['year']) && !isset($_GET['monthnum']) && !isset($_GET['category']) && !isset($_GET['string'])){

        $wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' => 'Press Release', 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
    }

    if( isset($_GET['year']) || isset($_GET['monthnum']) || isset($_GET['category']) || isset($_GET['string']) ){

        if( isset($_GET['year']) ){

            $year = $_GET['year'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',   'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['monthnum']) ){

            $monthnum = $_GET['monthnum'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',   'monthnum' => $monthnum,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
        }

        if( isset($_GET['category']) ){

            $category = $_GET['category'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post', 'category_name' =>  "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));
        }

        if( isset($_GET['string']) ){

            $string = $_GET['string'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post', '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['year']) && isset($_GET['monthnum']) ){

            $year = $_GET['year'];
            $monthnum = $_GET['monthnum'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  'monthnum' => $monthnum, 'year' => $year,'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }


        if( isset($_GET['year']) && isset($_GET['monthnum']) ){

            $year = $_GET['year'];
            $category = $_GET['category'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  'year' => $year,'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['year']) && isset($_GET['string']) ){

            $year = $_GET['year'];
            $string = $_GET['string'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  'year' => $year, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['monthnum']) && isset($_GET['category']) ){

            $monthnum = $_GET['monthnum'];
            $category = $_GET['category'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  'monthnum' => $monthnum, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['monthnum']) && isset($_GET['string']) ){

            $monthnum = $_GET['monthnum'];
            $string = $_GET['string'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  'monthnum' => $monthnum, '_name__like' => $string, 'category_name' => "Press Release", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

        if( isset($_GET['category']) && isset($_GET['string']) ){

            $string = $_GET['string'];
            $category = $_GET['category'];

            $wpb_all_query = new WP_Query(array('post_type'=>'post',  '_name__like' => $string, 'category_name' => "$category", 'post_status'=>'publish', 'posts_per_page'=>4, 'paged' => $paged ));

        }

    }

0 个答案:

没有答案