Wordpress自定义查询-顺序标题将无法工作

wordpress custom query - orderby title will not work

本文关键字:工作 标题 顺序 自定义 查询 Wordpress      更新时间:2023-09-26

我有一个问题,获得一个自定义查询按字母顺序排列。它一直默认按照发布日期的顺序显示。下面是我的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调用筛选列表之后,它们才不再按顺序排列。

我在谷歌上搜索了一段时间后发现了这个问题。我读到一些有这个问题的人发现他们的主题使用了一个叫做Post Types Order的插件。它覆盖了设置order参数的能力。

我看了看插件,果然,Post Types Order在那里。我读到的所有东西都说这个问题可以通过取消勾选插件设置中的"自动排序"来解决。然而,我这样做了,order仍然不起作用。我不得不完全停用插件,以获得订单标题的工作