使用WP_Query的Ajax Post Filter仅使用类别

时间:2016-11-22 05:58:01

标签: javascript php jquery ajax wordpress

我有一个以this article为模型的ajax后置过滤器。我主要按照需要工作,但我希望能够按类别过滤讲座(它实际上是一个子类别,但我不知道这是否有所不同)。

我有扬声器和最新讲座的过滤器,这些工作正常,但是当我尝试运行系列过滤器时(带有elseif的部分(isset($ _POST [' series_name'])))我没有回应。如果首先尝试运行系列过滤器,它将触发一次但不会响应。如果我先尝试运行任何其他过滤器,它们将正常工作,但系列过滤器根本不会启动。我认为我的问题是我在设置$ _POST [' series_name']时如何处理WP_Query的$ args。

非常感谢任何指示,谢谢!

这是我的php:     

function lectures_filter_function(){
$i = 1;
$args = array( 
    'post_type' => 'lectures',
    'orderby' => 'meta_value',
    'meta_key' => 'lecture_date',
    'order' => 'DESC'       
); 

if( isset( $_POST['speaker'] ) ) 
{
    $args['meta_query'][] = array(
        'key' => 'speaker',
        'value' => $_POST['speaker'],
    );
} 
elseif( isset( $_POST['series_name'] ) ) 
{
    //*This is how I wanted to code it but it didn't work at all*
    // $args['tax_query'] = array(
    //      array(
    //          'taxonomy' => 'category',
    //          'value' => 'category_name',
    //          'terms' => $_POST['series_name']
    //      )
    //  );

    //This is how I at least got it to fire once per screen load.
    $args = array(
                'category_name' => $_POST['series_name'],
                'post_type' => 'lectures',
                'orderby' => 'meta_value',
                'meta_key' => 'lecture_date',
                'order' => 'DESC'                   
        );      
}


    $the_query = new WP_Query( $args );
    $postlist = $the_query->post_count;

    // The Query

    // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post();

            // print_r($the_query);

            if($i % 2 == 1){
                echo '<div class="row">';
            }   
        ?>      

        <div class="large-6 columns lecture-tile">
            <div class="lecture-overlay-container">
                <a href="<?php the_permalink();?>">
                    <div class="lecture-tile-overlay"></div>
                    <?php the_post_thumbnail('full');?>
                    <div class="lecture-tile-info">
                        <h3><?php the_title();?></h3>

<?php 
    $current_id = get_the_id();
    $lecture_type = get_post_meta($current_id, 'lecture_type', true);
    if($lecture_type == "Single"){
        $lecture_descriptor = "lecture";
    }elseif ($lecture_type == 'Series') {
        $series_name = get_post_meta($current_id, 'series_name', true);
        $lecture_descriptor = $series_name;
    }elseif ($lecture_type == 'Event') {
        $event_name = get_post_meta($current_id, 'event_name', true);
        $lecture_descriptor = $event_name;
    }           
?>                              
                        <p><?php echo $lecture_descriptor; ?></p>                           
                    </div>
                </a>
            </div>
        </div>  

        <?php
                if($i % 2 == 0 || $i == $num_posts){
                    echo '</div>';
                }
                $i++;
        }
        /* Restore original Post Data */
        wp_reset_postdata();
    } else {
        // no posts found
        echo "No matching lectures found...";
    }
die();

}

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

1 个答案:

答案 0 :(得分:0)

试试这个

mongo admin --username root --password rootpassword --host ec2-xx-xxx-x-xxx.us-west-2.compute.amazonaws.com --port 27017

MongoDB shell version: XXX
connecting to: x.x.x.x:27017/admin

> db = db.getSiblingDB('databasename')
databasename
> db.createUser( { user: "username", pwd: "password", roles: [ "readWrite", "dbAdmin" ]} )
{
 "user" : "username",
 "pwd" : "...",
 "roles" : [
  "readWrite",
  "dbAdmin"
 ],
 "_id" : ObjectId("...")
}
> exit