最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

query posts - Help with query_posts function

programmeradmin4浏览0评论

I have a paid theme that has the following function manually hard-coded. It pulls the 8 latest posts and displays them:

global $do_not_duplicate;
global $post;

query_posts( array(
    'posts_per_page'        => '8',
    'post__not_in'          => $do_not_duplicate,
    'ignore_sticky_posts'   => 1
) );

I have looked at this page and tried to modify the code to show posts from the news category only (category number 1):

global $do_not_duplicate;
global $post;

// Attempt 1
query_posts( array(
    'cat=1',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 2
query_posts( array(
    'category=1',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 3
query_posts( array(
    'category' => array(1),
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 4
query_posts( array(
    'cat=news',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

Nothing worked. The same list of 8 recent posts from all categories was returned.

I have a paid theme that has the following function manually hard-coded. It pulls the 8 latest posts and displays them:

global $do_not_duplicate;
global $post;

query_posts( array(
    'posts_per_page'        => '8',
    'post__not_in'          => $do_not_duplicate,
    'ignore_sticky_posts'   => 1
) );

I have looked at this page and tried to modify the code to show posts from the news category only (category number 1):

global $do_not_duplicate;
global $post;

// Attempt 1
query_posts( array(
    'cat=1',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 2
query_posts( array(
    'category=1',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 3
query_posts( array(
    'category' => array(1),
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

// Attempt 4
query_posts( array(
    'cat=news',
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );

Nothing worked. The same list of 8 recent posts from all categories was returned.

Share Improve this question edited Nov 12, 2020 at 2:19 Matt asked Nov 12, 2020 at 1:52 MattMatt 1631 silver badge5 bronze badges 1
  • I strongly recommend against using query_posts, instead if you want to change the query, modify it with the pre_get_posts action, don't replace it, and if you absolutely must, use a standard WP_Query loop instead. There are no good situations to use query_posts and there are always better alternatives. If you find it in a premium theme this should be considered a red flag and a warning sign of poor code quality – Tom J Nowell Commented Nov 12, 2020 at 2:31
Add a comment  | 

1 Answer 1

Reset to default 0

The query_posts() function is almost wholly unnecessary and is discouraged in favor of WP_Query(). That being said we can look at the WP_Query() docs as they're almost the same.

The cat parameter accepts either a comma separated string or integer. In this case we can use:

query_posts( array(
    'cat' => 1,
    'posts_per_page' => '8',
    'post__not_in' => $do_not_duplicate,
    'ignore_sticky_posts' => 1
) );
发布评论

评论列表(0)

  1. 暂无评论