我写了一个WP\\u查询,其行为很奇怪。我几乎什么都试过了,但都没用。我找到了一个解决方案,但我试着去理解。
以下查询始终返回来自第一个caregory(id:15,slug:slug1)的帖子。
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category__in\' => array(15, 17),
\'posts_per_page\' => 4
);
$query = new WP_Query($args);
$items = $query->get_posts();
或
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'cat\' => \'15,17\',
\'posts_per_page\' => 4,
);
$query = new WP_Query($args);
$items = $query->get_posts();
或
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category_name\' => \'slug1,slug2\',
\'posts_per_page\' => 4,
);
$query = new WP_Query($args);
$items = $query->get_posts();
或
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 4,
\'tax_query\' => array(
array(
\'taxonomy\' => \'category\',
\'field\' => \'term_id\',
\'terms\' => array(15,17),
),
),
);
$query = new WP_Query($args);
$items = $query->get_posts();
解决方案是使用query\\u post($args)而不是WP\\u query->get\\u posts()
$args = array(
\'post_type\' => \'post\',
\'post_status\' => \'publish\',
\'category__in\' => array(15, 17),
\'posts_per_page\' => 4
);
$items = get_posts($args);
你能告诉我哪里错了吗?