1) 如果用户输入是
2) 如果用户输入是
我在WordPress工作,我需要在自定义类型的帖子上显示下一个和上一个链接。
我需要列出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
返回?
目前还不清楚你想要什么,但这里有两个猜测:
1) 如果用户输入是$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
。
2) 如果用户输入是$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
。
一般来说,在处理蛞蝓时,最好避免使用大写字母、空格和特殊字符。