douou0977 2012-02-23 18:51
浏览 20
已采纳

如何将自定义帖子类型查询的帖子数限制为3?

I am trying to display results of a custom post type on my main wordpress page.

This is my code so far:

                <?php
wp_reset_query();
$args=array(
  'post_type' => 'rooms',
  'post_status' => 'publish',
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {  ?>
    <TABLE border="1" cellspacing="50">
    <TR>FEATURED ACTIVE LISTINGS</tr>
  <?php
  while ($my_query->have_posts()) : $my_query->the_post(); 
    $my_custom_fields = get_fields(get_the_ID());
    if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Available'):
?>

    <tr><td><?php echo the_post_thumbnail('thumbnail');?>
    <br>UNIT <?php echo the_field('unit_number'); ?> <?php echo the_field('bedrooms'); ?>BEDS/<?php echo the_field('bathrooms'); ?>BA
    <br>$<?php echo the_field('price'); ?></td>
    </tr>
  <?php 
endif;
endwhile; ?>
  </TABLE>
<?php }
wp_reset_query();
?>

This works. However, If I try to add 'posts_per_page' => 3, into the array of args. It does not display any results at all. What am I doing wrong, or is there an alternative way to achieve this same result?

If it is relevant, The plugins I am using are Advanced Custom Fields and Custom Post Types.

Thanks in advance!

SOLVED:

I have actually figured it out myself, and thought I would share how I solved it.

'posts_per_page' => 3 was working but it would only show the last 3 posts of that type that was NOT being filtered by if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Available'):

In order to Limit posts that were being filtered by this field, I added my own counter and set it to be at max of 3. I changed the above line to if(isset($my_custom_fields['availability']) && $my_custom_fields['availability'] == 'Sold' && ($postcount < 3)): and added $postcount++; inside the loop.

Thanks again for your help. I hope this helps whoever else.

  • 写回答

2条回答 默认 最新

  • dsfsd43523 2012-02-24 22:15
    关注

    The solution you found is only going to work under certain conditions. If there aren't three posts with availability set to "Available" out of the set you retrieve (probably the first ten), you're not going to have enough. You can do a custom query instead that specifies a custom field name and value, as described in the WP_Query documentation:

    $args=array(
       'post_type' => 'rooms',
       'post_status' => 'publish',
       'meta_key' => 'availability',
       'meta_value' => 'Available',
       'posts_per_page' => 3,
       'ignore_sticky_posts'=> 1
    ); 
    $my_query = new WP_Query($args);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装