Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用类别id获取wordpress帖子?_Php_Wordpress_Plugins - Fatal编程技术网

Php 如何使用类别id获取wordpress帖子?

Php 如何使用类别id获取wordpress帖子?,php,wordpress,plugins,Php,Wordpress,Plugins,我在wordpress使用查询帖子时遇到问题 这是我的密码: $args = array('category__in' => array(9),'post_type'=>'banner-slider','post_status'=>'publish'); query_posts( $args ); while ( have_posts() ) : the_post(); the_title(); endwhile; 查询\返回后返回以下查询: SELECT SQL_CALC_

我在wordpress使用查询帖子时遇到问题

这是我的密码:

$args = array('category__in' => array(9),'post_type'=>'banner-slider','post_status'=>'publish');
query_posts( $args );
while ( have_posts() ) : the_post();
the_title();
endwhile;
查询\返回后返回以下查询:

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
在上面的查询中,我得到了0=1,这是错误的。但是,当我从查询帖子中删除类别时,我的查询工作正常


请告诉我哪里错了。

查询帖子不适用于插件或主题,更具体地说,法典说:

此功能不适用于插件或主题。作为 稍后解释,有更好、更高性能的选项可供更改 主查询。query_posts()是一种过于简单且有问题的方法 通过将页面的主查询替换为的新实例来修改该页面的主查询 查询。这是低效的(重新运行SQL查询),并且会彻底失败 在某些情况下失败(尤其是在处理帖子时) 分页)。任何现代WP代码都应该使用更可靠的方法,如 为此目的,使用pre_get_杆钩

引自:

WordPress codex建议使用
WP\u Query
get\u posts()
。在您的例子中,我认为
get_posts()
应该足够了,所以这个示例将使用它。下面的示例实际上来自WordPress codex,对您的
类别
post\u类型
post\u状态
进行了一些修改:

<?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();
?>

  • 法典: