我需要向术语添加元数据(例如 update_post_meta )。
我创建了 wp_termmeta
(使用 this tutorial 进行一些修改)表,如 wp_postmeta
。
所以现在这个机制运行良好,我可以使用get_metadata向任何术语添加/获取/删除元数据。/update_metadata/delete_metadata .
我可以根据术语对象/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/