dr5648 2014-09-04 14:11
浏览 32
已采纳

我如何使用类别ID获取wordpress帖子?

I am getting problem in wordpress using query_posts.

Here is my code:

$args = array('category__in' => array(9),'post_type'=>'banner-slider','post_status'=>'publish');
query_posts( $args );
while ( have_posts() ) : the_post();
the_title();
endwhile;

query_post return following query:

SELECT SQL_CALC_FOUND_ROWS  bib_posts.* FROM bib_posts  WHERE 1=1  AND 0 = 1 AND bib_posts.post_type = 'banner-slider' AND ((bib_posts.post_status = 'publish')) GROUP BY bib_posts.ID ORDER BY bib_posts.post_date DESC LIMIT 0, 10

In the above query, i am getting 0=1 which is wrong. but when i remove category__in from query post then my query working fine.

Please suggest me where I am wrong.

  • 写回答

1条回答 默认 最新

  • dongque1462 2014-09-04 14:20
    关注

    query_posts is not meant to be used by plugins or themes, more specifically the codex says:

    This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of pre_get_posts hook, for this purpose.

    Quoted from: http://codex.wordpress.org/Function_Reference/query_posts

    query_posts

    The WordPress codex recommends using WP_Query or get_posts(). In your case, I think get_posts() should be sufficient, so this example is going to use that. The below example is actually from the WordPress codex, with a couple of modifications for your category, post_type, and post_status:

    <?php
    $args = array( 'category' => 9, 'post_type' => 'banner-slider', 'post_status' => 'publish' );
    
    $myposts = get_posts( $args );
    foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php
    endforeach; 
    wp_reset_postdata();
    ?>
    

    Codex: http://codex.wordpress.org/Template_Tags/get_posts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)