无法按类别获取产品,只能获取最后添加的产品

时间:2016-08-06 作者:Mark Alan

我创建了一个查询,其中产品是按产品子类别名称或产品标签获取的,但主要问题是,我只获取最后输入的产品。有人能帮我解决问题吗?请告诉我我做错了什么,我做错了什么。

<?php 
if(isset($_POST[\'step_03\'])) {
    $que_01 = $_POST[\'que_01\'];
    $que_02 = $_POST[\'que_02\'];
    $que_03 = $_POST[\'que_03\'];
    $subcat = array("$que_01", "$que_02", "$que_03");

    $args = array( 
        \'post_type\'      => \'product\', 
        \'posts_per_page\' => 1, 
        \'product_tag\'    => $que_01 
    );
} else {
    if(isset($_POST[\'cat\'])) {
        $tags = $_POST[\'cat\'];
    }
    $args = array( 
        \'post_type\'      => \'product\', 
        \'posts_per_page\' => 1, 
        \'product_category\'   => $tags 
    );
}
$loop = new WP_Query( $args );
$product_count = $loop->post_count;
if( $product_count > 0 ) :
    while ( $loop->have_posts() ) : $loop->the_post(); 
        global $post;
        echo $thePostID = $loop->post->post_title;
            }
endif;
?>

1 个回复
SO网友:Akshay Shah

Try this code.

<?php 


if(isset($_POST[\'step_03\'])) {
    $que_01 = $_POST[\'que_01\'];
    $que_02 = $_POST[\'que_02\'];
    $que_03 = $_POST[\'que_03\'];
    $subcat = array("$que_01", "$que_02", "$que_03");

    $args = array( 
        \'post_type\'      => \'product\', 
        \'posts_per_page\' => -1, 
        \'product_tag\'    => $que_01 
    );
} else {
    if(isset($_POST[\'cat\'])) {
        $tags = $_POST[\'cat\'];
    }
    $args = array( 
        \'post_type\'      => \'product\', 
        \'posts_per_page\' =>  -1, 
        \'product_category\'   => $tags 
    );
}
$loop = new WP_Query( $args );
$product_count = $loop->post_count;
if( $product_count > 0 ) :
    while ( $loop->have_posts() ) : $loop->the_post(); 
        global $post;
        echo $thePostID = $loop->post->post_title;
            }
endif;


?>

相关推荐