wordpress - 如何根据 TERM 元键获取术语和帖子?

标签 wordpress metadata

我需要向术语添加元数据(例如 update_post_meta )。

我创建了 wp_termmeta (使用 this tutorial 进行一些修改)表,如 wp_postmeta

所以现在这个机制运行良好,我可以使用get_metadata向任何术语添加/获取/删除元数据。/update_metadata/delete_metadata .

enter image description here

我可以根据术语对象/ID等获取术语元数据。但我需要根据术语元键获取术语/帖子

例如:

我有 CPT 。然后我有自定义税务writer。现在我正在向 writer - featured 添加元数据,其可能值为 true/false (1/0)

现在我有两个问题:

1) 如何获取所有推荐的作家(writer 税项)(featured = true)?

2) 如何从 CPT book 获取属于精选作家的书籍?

附注1

这里是get_posts功能。使用 args 我可以根据帖子元键获取帖子:

<?php $args = array(
'meta_key'         => '',
'meta_value'       => ''); ?>

还有get_terms函数但没有元键参数。

也许WP_Query中有一些解决方案来获取我需要的东西?也许没有内置的解决方案可以满足我的需求?

最佳答案

如何获取所有精选作家(作家税条款)(精选 = true)?

为此,您首先需要获取特色术语的 ID,然后获取这些术语。

$featured_writers = get_featured_writers();

如何从 CPT 图书中获取有特色作家的图书?

为此,我建议首先获取特色术语的 ID,然后创建 WP_Query与该术语相关的书籍的对象。然后您可以使用这个 WP_Query The Loop 中的对象.

$featured_books = get_books_with_featured_writer();

如果您既想要特色作家又想要特色作家的书籍

为了保存重复的查询(尽管 WP 非常擅长缓存它们),如果您既需要特色作家又需要特色作家的书籍,则可以这样做 -

$featured_writers_ids = get_featured_writer_ids();
$featured_writers = get_featured_writers($featured_writers_ids);
$featured_books = get_books_with_featured_writer($featured_writers_ids);

代码

将此代码放入您的 functions.php 文件(或 functions.php 包含的任何文件)中。

尝试一下,如果遇到问题请告诉我 -

function get_featured_writers($featured_writers_ids = null){

    /** Ensure the $featured_writers_ids are set and valid */
    $featured_writers_ids = check_features_writer_ids($featured_writers_ids);
    if(!$featured_writers_ids) :
        return false;
    endif;

    $args = array(
        'fields'    => 'ids',
        'include'   => $featured_writers_ids
    );
    $featured_writers = get_terms('writer', array('include' => $featured_writers_ids));

    return $featured_writers;

}

function get_books_with_featured_writer($featured_writers_ids = null){

    /** Ensure the $featured_writers_ids are set and valid */
    $featured_writers_ids = check_features_writer_ids($featured_writers_ids);
    if(!$featured_writers_ids) :
        return false;
    endif;

    $args = array(
        'post_type' => 'book',
        'tax_query' => array(
            array(
                'taxonomy' => 'writer',
                'field' => 'id',
                'terms' => $featured_writers_ids
            )
        )
    );
    $books = new WP_Query($args);

    return $books;

}

function get_featured_writer_ids(){

    global $wpdb;

    // This query assumes you've added `termmeta` to the `$wpdb` global. Replace with `$wpdb->termmeta` with `'wp_termmeta'` if you have not.
    $query  = $wpdb->prepare("SELECT `%1$s`.`term_id` FROM %1$s WHERE `meta_key` = 'term_featured' AND `meta_value` = '1'", $wpdb->termmeta);
    $featured_writers_ids = $wpdb->get_col($query);

    return $featured_writers_ids;

}

function check_features_writer_ids($featured_writers_ids = null){

    /** Check to see if any featured writers were passed, or if they should be grabbed now */
    if(is_null($featured_writers_ids)) :
        $featured_writers_ids = get_featured_writer_ids();
    endif;

    /** Ensure that there are featured writers, and that $featured_writers_ids is an array */
    if(is_empty($featured_writers_ids) || !is_array($featured_writers_ids)) :
        return false;
    endif;

    return $featured_writers_ids;

}

关于wordpress - 如何根据 TERM 元键获取术语和帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20241669/

相关文章:

javascript - 未捕获的类型错误 : Property '$' of object - jQuery

wordpress - 有没有办法在ACF图像字段中添加默认图像

python - 无法使用Python从视频文件中获取正确的Exif数据

git - git 是否跟踪资源 fork ?

javascript - 更改特定 GIF 帧的背景

wordpress - 如何将 .htaccess 规则/内容移动到虚拟主机?

javascript - Wordpress 何时将 hide-if-js 类添加到元数据框

video - ffmpeg 在视频编码时忽略 -metadata 选项

metadata - MPEG DASH 中的定时元数据?

sql-server - 列系统表(sys.columns、sys.all_columns 等)如何 self 描述?