WP_QUERY未返回预期结果

时间:2015-02-14 作者:user3515709

I have 2 dropdown lists, when a user select value in the first list, there is an ajax call to a function using WP_query which send results to populate the second dropdown.

My WP_query is not returning any result. I\'m new to WP_query so I should have made a mistake somewhere.

The query should return the posts having the meta_key _wpcf_belongs_marque-type_id with the meta_value equal to the parent id sent by ajax. The posts should also have the post_type \'aromes-type\'.

Here is the function located in my functions.php:

wp_enqueue_script( \'my-ajax-request\', get_template_directory_uri() . \'/js/ajax.js\', array( \'jquery\' ) );
wp_localize_script( \'my-ajax-request\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
add_action( \'wp_ajax_brand_children\', \'GetBrandChildren\');
add_action( \'wp_ajax_nopriv_brand_children\', \'GetBrandChildren\');



function GetBrandChildren() {

    $output = \'\';
    //retrieve POST data sent by AJAX
    $parent_id = $_GET[\'parent_id\'];

    //Define query arguments
    $args = array(
    \'post_type\'  => \'aromes-type\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
                array(
                    \'key\'     => \'_wpcf_belongs_marque-type_id\',
                    \'value\'   => $parent_id
                ),
            ),
    \'posts_per_page\' => -1
    );


    //Create the query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
                $output .= \'<option>\' . get_the_title() . \'</option>\';
        endwhile;
    else:
            $output = \'<option>No flavors found...</option>\';
    endif;

    echo $output;

    // Reset Post Data
    wp_reset_postdata();
    wp_die();
}

The JQuery making the call :

//On selected brand, update flavors list
  $(document).on(\'change\', "select[id^=\'marque\']", function() {

        var $brandid =  $(this).val();
        var $brand_dd_id = $(this).attr(\'id\');
        var $flav_dd_id = $brand_dd_id.substr($brand_dd_id.length-1);

        //Make AJAX request, using the selected value as the GET
        $.ajax({
                url: MyAjax.ajaxurl,
                beforeSend: function(){$("#arome"+$flav_dd_id+".ul.select2-results").empty();},
                data: {
                        \'parent_id\': $brandid,
                        \'action\': \'brand_children\'
                      },
                success: function(output) {
                    console.log(output);
                    $("#arome"+$flav_dd_id+".ul.select2-results").append(output);
                    $("#arome"+$flav_dd_id).trigger("chosen:updated");
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status + " "+ thrownError);
        }});

});
2 个回复
最合适的回答,由SO网友:Bruno Rodrigues 整理而成

尝试使用此选项:

$args = array(
    \'post_type\' => YOUR_POST_TYPE,
    \'meta_query\' => array(
        array(
            \'key\'     => \'_wpcf_belongs_marque-type_id\',
            \'value\'   => $parent_id
        )
    )
);
在循环中设置else 语句以确保查询不会返回零结果:

if ( $the_query->have_posts() ) :
    while ( $the_query->have_posts() ) : $the_query->the_post();
        $output .= \'<li class="active-results">\' . the_title() . \'</li>\';
    endwhile;
else:
    $output = \'No items here :(\';
endif;
希望有帮助。

SO网友:user3515709

你说得对,Bruno,AND是可选的,我在没有它的情况下尝试过,现在它可以工作了,不知道为什么,下面是代码:

function GetBrandChildren() {

    $output = \'\';
    //retrieve POST data sent by AJAX
    $parent_id = $_GET[\'parent_id\'];

    //Define query arguments
    $args = array(
    \'post_type\'  => \'aromes-type\',
    \'meta_query\' => array(
                array(
                    \'key\'     => \'_wpcf_belongs_marque-type_id\',
                    \'value\'   => $parent_id
                ),
            ),
    \'posts_per_page\' => -1
    );


    //Create the query
    $the_query = new WP_Query( $args );

    // The Loop
    if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
                $output .= \'<option>\' . get_the_title() . \'</option>\';
        endwhile;
    else:
            $output = \'<option>No flavors found...</option>\';
    endif;

    echo $output;

    // Reset Post Data
    wp_reset_postdata();
    wp_die();
}

结束

相关推荐

如何使用AJAX获取模板部件?

我目前正在使用Woocommerce存档产品。php模板文件(比如商店页面或产品类别页面)。单击文章(或产品)后,将向同一页面的标题添加GET查询,将页面更改为滑块而不是文章列表。我需要使用AJAX更改页面的内容,而不是刷新和添加GET查询。我尝试包含模板部分,但它一直说找不到帖子。我一直在读,我可能需要加载wp-load。php或其他方法。最有效的方法是什么?我想学习如何获取\\u template\\u part()以返回特定URL上的内容。(稍后我需要使用类似的方法检索single-product.