Wordpress订单自定义帖子类型

问题描述:

我希望有人可以帮助我。我是PHP新手,所以在工作上非常了解。Wordpress订单自定义帖子类型

我正在自定义一个现有的WordPress主题,但我面临主题高级搜索/结果的问题。

主题无法订购搜索结果,我知道Wordpress的默认订单是按日期排列的。目前,如果我执行搜索,结果将按日期顺序显示,但我需要结果是价格从高到低。

当前的代码如下

<?php /* If there are no posts to display, such as an empty archive page */ ?> 
<?php if (! have_posts()) : ?> 
    <article id="post-0" class="post error404 not-found"> 
     <h1 class="posttitle"><?php _e('Not Found', THE_LANG); ?></h1> 
     <div class="entry"> 
      <p><?php _e('Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG); ?></p> 
     </div> 
    </article> 
<?php endif; ?> 

<div class="nvr-prop-container row"> 
<?php if(have_posts()){ ?> 
    <div class="search-title twelve columns"> 
     <h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4> 
    </div> 

    <?php 
    $nvr_idnum = 0; 
    $nvr_typecol = "nvr-prop-col"; 
    $nvr_imgsize = "property-image"; 
    ?> 
    <ul id="nvr-prop-search" class="<?php echo esc_attr($nvr_typecol); ?>"> 

    <?php 
    while (have_posts()) : the_post(); 
      $nvr_idnum++; 

      echo nvr_prop_get_box($nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace); 


      $nvr_classpf=""; 

    endwhile; // End the loop. Whew. 
    ?> 

然后我决定尝试对结果进行排序,所以我创建

$sort_properties = new WP_Query(array(
    'post_type'   => 'properties', 
    'meta_key'   => $nvr_initial.'_price', 
    'meta_value'  => $nvr_price, 
    'orderby'   => 'meta_value_num date', 
    'order'    => 'DESC', 
)); 

<?php /* If there are no posts to display, such as an empty archive page */ ?> 
    <?php if (! have_posts()) : ?> 
     <article id="post-0" class="post error404 not-found"> 
      <h1 class="posttitle"><?php _e('Not Found', THE_LANG); ?></h1> 
      <div class="entry"> 
       <p><?php _e('Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG); ?></p> 
      </div> 
     </article> 
    <?php endif; ?> 

    <div class="nvr-prop-container row"> 
    <?php if($sort_properties->have_posts()){ ?> 
     <div class="search-title twelve columns"> 
      <h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4> 
     </div> 

     <?php 
     $nvr_idnum = 0; 
     $nvr_typecol = "nvr-prop-col"; 
     $nvr_imgsize = "property-image"; 
     ?> 
     <ul id="nvr-prop-search" class="<?php echo esc_attr($nvr_typecol); ?>"> 

     <?php 
     while ($sort_properties->have_posts()) : $sort_properties->the_post(); 
       $nvr_idnum++; 

       echo nvr_prop_get_box($nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace); 


       $nvr_classpf=""; 

     endwhile; // End the loop. Whew. 
     ?> 

现在,当我执行搜索,职位进行排序基于价格这是太棒了,但...现在无论我如何搜索所有网站帖子现在正在显示。

我觉得我是如此接近寻找解决方案,但我会非常感激,如果有人感冒用这个建议我。

亲切的问候

小号

我认为你必须添加Search Parameter,可能使用新的meta_query结构:

$search_query = get_search_query(); 
$sort_properties = new WP_Query(array(
    'post_type'   => 'properties', 
    's'     => $search_query, 
    'orderby'   => 'meta_value_num', 
    'meta_query' => array(
     'key'   => $nvr_initial.'_price', 
     'value'  => $nvr_price, 
    ) 
)); 

我记得在遇到一些麻烦,不使用 “meta_query”= > array()用于多个条件。

+0

感谢这一点,但它并没有做任何的结果。 – user4572969

+0

编辑我的答案并添加了meta_query => array()部分。 – noreabu

+0

你好,谢谢,但我担心它不会影响结果。 post_count; ?>显示正确的帖子数量,但显示的实际帖子保持不变。 – user4572969

您是否尝试过只用“meta_val_num”没有“日期”来样订货:从

$args = array(
    'post_type' => 'product', 
    'orderby' => 'meta_value_num', 
    'meta_key' => 'price', 
); 
$query = new WP_Query($args); 

代码:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters