gpt4 book ai didi

mysql - WordPress 自定义类型 Posts next previous join table

转载 作者:行者123 更新时间:2023-11-29 02:24:25 25 4
gpt4 key购买 nike

我在 WordPress 工作,我需要在自定义类型的帖子上显示下一个和上一个链接。

我需要在表中的 post_town 上列出所有自定义帖子类型

cus_builders

ID     post_town    post_type 
-----------------------------------

123 Manchester cus_builders

下面的代码为我提供了所有自定义帖子 cus_builders,我只想要曼彻斯特的 build 者。

// 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


一般来说,在处理 slug 时避免使用大写字母、空格和特殊字符是个好主意。

关于mysql - WordPress 自定义类型 Posts next previous join table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25523173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com