wordpress自定义查询 - orderby标题将无法正常工作

时间:2015-05-28 18:46:40

标签: javascript php jquery ajax wordpress

我在使用自定义查询进行字母排序时遇到问题。它保持默认显示按发布日期的顺序显示。下面是我的php函数。

function json_info2() {

    // The $_REQUEST contains all the data sent via ajax
    if ( isset($_REQUEST) ) {

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

    // get values for all three drop-down menus
    $status = $_REQUEST['status'];
    $industry = $_REQUEST['services'];
    $state = $_REQUEST['state']; 


    // array of values for each of the three drop-downs
    $statusAll = array('complete','incomplete');
    $industryAll = array('mining','textile','machinery');
    $statesAll = array('SC','TX','WA');

    // set $statusArray dependent on whether or not "all" is selected in the dropdown menu
    if($status == "all") {
        $statusArray = array( 'key' => 'status', 'value' => $statusAll, 'compare' => 'IN');
    } else {
        $statusArray = array( 'key' => 'status', 'value' => $status, 'compare' => '=');
    }

    if($industry == "all") {
        $industryArray = array( 'key' => 'industry', 'value' => $industryAll, 'compare' => 'IN');
    } else {
        $industryArray = array( 'key' => 'industry', 'value' => $industry, 'compare' => '=');
    }

    if($state == "all") {
        $stateArray = array( 'key' => 'state', 'value' => $statesAll, 'compare' => 'IN');
    } else {
        $stateArray = array( 'key' => 'state', 'value' => $state, 'compare' => '=');
    }


        $pages = array(
            'post_type' => 'page',
            'orderby' => 'title',
            'order' => 'ASC',
            'paged' => $paged,
            'posts_per_page' => 5,
            'meta_query'    => array(
                                    'relation'      => 'AND',
                                    $statusArray,
                                    $industryArray,
                                    $stateArray,    
                                        array(
                                        'key'       => '_wp_page_template',
                                        'value'     => 'template-individual-project.php',
                                        'compare'   => '='
                                    )
                                )
        );

        // query results by page template
        $my_query = new WP_Query($pages);


        if($my_query->have_posts()) : 
                while($my_query->have_posts()) : 
                    $my_query->the_post();  

                    <li>
                        <?php the_title(); ?>
                    </li>

                    <?php

            endwhile;endif;

            wp_reset_query();

         } // end of isset

                    ?>

         <?php
             die();
}

add_action( 'wp_ajax_json_info2', 'json_info2' );
add_action( 'wp_ajax_nopriv_json_info2', 'json_info2' );
         ?>

以上函数由以下ajax函数调用:

function do_ajax() {
// Get values from all three dropdown menus
        var state = $('#states').val();
        var markets = $('#markets').val();
        var services = $('#services').val();
            $.ajax({
                url: ajaxurl, 
                data: {
                    'action' : 'json_info2',
                    'state' : state,
                    'status' : markets,
                    'services' : services
                },
                success:function(moredata) {
                    // This outputs the result of the ajax request
                    $('#project-list').html( moredata );
                    $('#project-list').fadeIn();
                }/*,
                error: function(errorThrown){
                    var errorMsg = "No results match your criteria";
                    $('#project-list').html(errorMsg);
                }*/
            }); // end of ajax call
        } // end of function do_ajax

我在这里缺少一些简单的东西吗?加载时我在页面上有一个类似的自定义查询(虽然初始加载查询没有选择菜单值作为args),它们按字母顺序显示就好了。只有在ajax调用过滤列表后,它们才会按顺序排列。

1 个答案:

答案 0 :(得分:0)

我在搜索问题很长一段时间后发现了这个问题。我读到一些遇到此问题的人发现他们的主题是使用名为Post Types Order的插件。它会覆盖设置orderby arg的功能。

我查看了插件,果然,Post Types Order就在那里。我读到的所有内容都表明,问题可以通过取消选中&#34; auto sort&#34;来解决。在插件的设置中。但是,我这样做了,而且仍然没有工作。我不得不完全停用插件以使orderby标题起作用。