我正在 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
。
一般来说,在使用 slugs 时避免使用大写字母、空格和特殊字符是个好主意。
关于mysql - WordPress 自定义类型帖子下一个上一个连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523173/