如何根据TERM元键获取条款和帖子?

时间:2013-11-27 11:49:01

标签: wordpress metadata term

我需要将元数据添加到术语(类似update_post_meta)。

我创建了wp_termmeta(使用this tutorial进行了一些修改)表{@ 1}}。

所以现在这种机制运作良好,我可以使用get_metadata / update_metadata / delete_metadata向任何字词添加/获取/删除元数据。

enter image description here

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

例如:

我有CPT wp_postmeta。然后我有自定义税book。现在,我将元数据添加到writer - writer,其值可能为featured

现在我有2个问题:

1)如何获得所有作者(true/false (1/0)税的条款)的特色(writer = featured)?

2)Ho从CPT获取书籍true属于作者特色?

P.S。 1

以下是get_posts功能。使用args我可能会根据帖子元键获得帖子:

book

此外还有get_terms功能,但没有元关键参数。

也许在<?php $args = array( 'meta_key' => '', 'meta_value' => ''); ?> 中有一些解决方案来获取我需要的东西?也许没有针对我需求的内置解决方案?

1 个答案:

答案 0 :(得分:1)

如何获得所有作者(作家税的条款)的特色(精选=真)?

为此,您首先需要获取特色术语的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;

}