我在WordPress工作,我需要在自定义类型的帖子上显示下一个和上一个链接。
我需要列出表{/ p>中post_town
上的所有自定义帖子类型
cus_builders
ID post_town post_type
-----------------------------------
123 Manchester cus_builders
以下代码为我提供了所有自定义帖子cus_builders,我只想要曼彻斯特的建设者。
// get_posts in same custom taxonomy
$postlist_args = array(
'posts_per_page' => -1,
'post_type' => 'cus_builders',
'post_town' => 'Manchester'
);
$postlist = get_posts( $postlist_args );
如何在表cus_builders
上加入查询,以便它只根据cus_builder.post_town
回复?
答案 0 :(得分:2)
目前尚不清楚你的目标是什么,但这里有两个猜测:
$post_town
:// Input:
$post_town = 'Manchester';
// Query:
$postlist_args = array(
'posts_per_page' => -1,
'post_type' => get_post_type_by_town( $post_town ),
'tax_query' => array(
array(
'taxonomy' => 'post_town',
'terms' => sanitize_key( $post_town ),
'field' => 'slug',
),
),
);
$postlist = get_posts( $postlist_args );
其中
function get_post_type_by_town( $post_town )
{
global $wpdb;
$sql = "SELECT post_type FROM cus_builders WHERE post_town = '%s'";
return $wpdb->get_col( $wpdb->prepare( $sql, $post_town ) );
}
此设置需要多种自定义帖子类型,共享相同的自定义分类post_town
。
$post_type
:// Input:
$post_type = 'cus_builders';
// Query:
$postlist_args = array(
'posts_per_page' => -1,
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => 'post_town',
'terms' => get_post_towns_by_post_type( $post_type ),
'field' => 'slug',
),
)
);
$postlist = get_posts( $postlist_args );
其中:
function get_post_towns_by_post_type( $post_type )
{
global $wpdb;
$sql = "SELECT post_town FROM cus_builders WHERE post_type = '%s'";
return $wpdb->get_col( $wpdb->prepare( $sql, $post_type ) );
}
此设置需要自定义分类post_town
。
使用slu g时,避免使用大写字母,空格和特殊字符通常是一个好主意。