mysql - WordPress 自定义类型帖子下一个上一个连接表

标签 mysql wordpress

我正在 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/

相关文章:

PHP 和 MySQL 选择单个值

php - 我的代码中哪种语法是正确的?

c# - 使用从数据库检索的 DateTime 进行减法给出奇怪的结果

javascript - Php 抛出 SQLi 计数错误

php - 如何在 Propel 1.6 中按日期过滤日期时间列?

php - WordPress 自定义 REST API

php - <p> 文本上的滚动条

wordpress - 全新安装 : httpd. 服务:未找到单元

php - WordPress 西里尔文编码

php - 有没有人有一个很好的功能来删除 Wordpress 中的特定简码?