我正在为 wordpress 中的每个类别创建新页面。帖子编辑器有一个自定义字段,允许选择扇区类型,这会在更新时应用于帖子。自定义字段key
是:sector
,对于自定义字段元value
选项,可以使用SectorA
,SectorB
和SectorC
。我正在使用一个名为projects
.
如何更改下面代码中的查询行,以便按扇区名称过滤循环,让我们使用SectorA
. 然后,我将重用每个模板页面上的代码,将值更改为SectorB
其他SectorC
页面。
我认为这需要以某种方式改变:
$customPosts->query('showposts=5§or=sectorA&post_type=projects' );
目前它成功地呼应了sector value
并且description value
显示了所有帖子。所以我尝试将其限制在扇区A 使用sector=sectorA
似乎不起作用?
这段代码在functions.php中:
function get_custom_field_posts_join($join) {
global $wpdb, $customFields;
return $join . " JOIN $wpdb->postmeta postmeta ON (postmeta.post_id = $wpdb->posts.ID and postmeta.meta_key in ($customFields)) ";
}
function get_custom_field_posts_group($group) {
global $wpdb;
$group .= " $wpdb->posts.ID ";
return $group;
}
这段代码在模板页面上:
<?php /* Begin Custom Field Posts */ ?>
<h2>Custom Posts</h2>
<ul>
<?php
global $customFields;
$customFields = "'sector', 'description'";
$customPosts = new WP_Query();
add_filter('posts_join', 'get_custom_field_posts_join');
add_filter('posts_groupby', 'get_custom_field_posts_group');
$customPosts->query('showposts=5§or=sectorA&post_type=projects' );//Uses same parameters as query_posts
remove_filter('posts_join', 'get_custom_field_posts_join');
remove_filter('posts_groupby', 'get_custom_field_posts_group');
while ($customPosts->have_posts()) : $customPosts->the_post();
$sector = get_post_custom_values("sector");
$description= get_post_custom_values("description");?>
<li><?php echo $sector[0]; ?></li>
<li><?php echo $description[0]; ?></li><br />
<?php endwhile; ?>
</ul>
<?php /* End Custom Field Posts */ ?>
谢谢你的帮助