我有这个类别是“社区帖子”我不希望它出现在我的主页上,所以我将其添加到我的查询中
function upload(files) {
var uploadCount = files.length;
function cycle(files) {
var promises = [];
for (var i = 0; i < files.length; i++) {
var file = files[i];
var promise = Upload.upload({
url: '/photos.json',
file: file
});
promises.push(promise);
};
return $q.all(promises);
};
cycle(files).then(function(result) {
if(uploadCount > 1) {
$scope.lastAction = 'uploaded ' + uploadCount + ' photos';
} else {
$scope.lastAction = 'uploaded ' + uploadCount + ' photo';
};
$scope.showSuccessModal = true;
$scope.uploading = false;
$scope.failedUploads = [];
$scope.newPhotos = {
token: $scope.token,
files: undefined
};
photoUploadBtn.removeClass('disabled');
getPhotos(q);
})
};
这对我很好,但有些“社区帖子”我想让它们在主页循环中显示。 (例外)
所以我想只将具有一个类别的帖子排除为“社区帖子”,如果它具有此类别且通常显示的更多。
答案 0 :(得分:1)
第一件事do not use query_posts - 永远不应该使用它,因为它改变了主查询。请改用get_posts
- 它更安全,执行相同的任务。
要回答您的问题,让我们首先想象一下查询在SQL中的外观(假设您的$id_communityposts
等于2):
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_postmeta
LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
WHERE
wp_posts.ID = wp_postmeta.post_id AND
(
(wp_term_taxonomy.taxonomy = 'category' AND wp_term_taxonomy.term_id NOT IN(2))
OR
(wp_postmeta.meta_key = 'featured' AND wp_postmeta.meta_value = 1)
)
ORDER BY wp_posts.post_date DESC
LIMIT 4
因此,我们查询帖子,发布元和分类表并提出两个可能的条件:
featured
元键设置为1
(根据您存储&#34;特色&#34;信息的方式,将其更改为任何键/值)。对于那种特定情况,get_posts
并不是一件好事 - 使用WPDB
查询数据库会给你更大的灵活性。
$posts = $wpdb->get_results(
"SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->posts.ID = $wpdb->postmeta.post_id AND
(
($wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id NOT IN(2))
OR
($wpdb->postmeta.meta_key = 'featured' AND $wpdb->postmeta.meta_value = 1)
)
ORDER BY $wpdb->posts.post_date DESC
LIMIT 4"
);
如果您遇到任何问题,请告诉我,因为这是一个未经测试的查询。
答案 1 :(得分:0)
如果我正确地理解了这个问题,那么最简单的解决方案,不涉及复杂的SQL
将会是这样的:
// NOT TESTED !
if ( count(get_the_category()) > 1 ) { // this means there are more than single category
// show the desired posts
} else {
// dont show
}
在codex中阅读get_the_category()
沿着相同的逻辑线,您也可以使用wp_get_post_categories