admin管理员组文章数量:1388700
For example:
function getBlogFeedPosts($settings, $featuredPosts, $exclude_array, $numPosts, $offset)
{
$mbaQueryArgs = [
'post_type' => 'post',
'posts_per_page' => $numPosts,
'orderby' => 'date',
'offset' => $offset,
'posts_per_page' => $numPosts,
'exclude' => $exclude_array,
];
if (!empty($settings->blogs)) {
$mbaQueryArgs['meta_query'] = [
[
'key' => 'feed_id',
'value' => $settings->blogs,
'compare' => 'IN',
],
];
}
$query = new WP_Query($mbaQueryArgs);
return $query;
}
and
function getRegCatPosts($settings, $featuredPosts, $exclude_array,
$numPosts, $offset)
{
$mbaQueryArgs = [
'post_type' => 'post',
'posts_per_page' => $numPosts,
'orderby' => 'date',
'offset' => $offset,
'posts_per_page' => $numPosts,
'exclude' => $exclude_array,
];
if (!empty($settings->categories) || !empty($settings->regions)) {
$mbaQueryArgs['tax_query'] = ['relation' => 'OR'];
if (!empty($settings->regions)) {
array_push($mbaQueryArgs['tax_query'], [
'taxonomy' => 'region',
'field' => 'id',
'terms' => $settings->regions,
]);
}
if (!empty($settings->categories)) {
array_push($mbaQueryArgs['tax_query'], [
'taxonomy' => 'category',
'field' => 'id',
'terms' => $settings->categories,
]);
}
}
$query = new WP_Query($mbaQueryArgs);
return $query;
}
I'm calling them like this to be merged later on like so:
$queryBlogFeeds = getBlogFeedPosts($settings, $featuredPosts, $exclude_array, -1, 0);
$queryRegCat = getRegCatPosts($settings, $featuredPosts, $exclude_array, -1, 0);
$blogFeedPosts = $queryBlogFeeds->posts;
$regCatPosts = $queryRegCat->posts;
// Get the IDs from each query
$feedsPosts = wp_list_pluck($blogFeedPosts, 'ID');
$taxonomiesPosts = wp_list_pluck($regCatPosts, 'ID');
//Merge the results and filter out duplicate posts
$postIds = array_merge($feedsPosts, $taxonomiesPosts);
the queries are given post ids from user selection on front-end. the problem is that if a user doesn't make a selection that passes data to a query function, that function returns all posts. I understand that parameter '-1' on the functions is to return all posts, but that what I need in this case. I think the issue might be how i'm storing the functions in $queryRegCat and $regCatPosts, but I'm not having any luck here trying to modify that piece or find any solutions here that fix what I'm trying to do.
本文标签: phpHow to prevent WPQuery function from returning all posts when empty
版权声明:本文标题:php - How to prevent WP_Query function from returning all posts when empty? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744545112a2611849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论