获取术语和元值结果为空(_T)

时间:2016-12-08 作者:Noob Theory

目前,在我的函数php中有这个

    $args = array (
\'post_type\' => \'property\',
\'meta_key\' => \'property_channel\',
\'meta_value\' => \'1\',
\'orderby\' => \'name\',
\'order\' => \'ASC\',
\'hide_empty\' => 1

 );
 $terms = get_terms("property-city", $args);
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
 foreach ( $terms as $term ) { ?>
    <li class="calendar-filter-menu-item" data-filter=".<?php echo $term-    >slug; ?>"><?php echo $term->name; ?></li>
   <?php }
  }
结果为空。如果我注释掉meta\\u键和meta\\u值,它会显示税。。。但我需要显示当前的属性城市列表和meta\\u值1。

1 个回复
最合适的回答,由SO网友:CodeMascot 整理而成

在这里,我为您编写了一个基于sql的函数。通过将参数作为数组传递,您将获得带有slug和name的术语ID-

function the_dramatist_get_terms_with_post_meta( $args = array() ) {
    global $wpdb;

    $default_args = array(
        \'taxonomy\' => \'category\',
        \'meta_key\' => \'\',
        \'meta_value\' => \'\',
        \'post_type\' => \'post\'
    );

    $param = wp_parse_args( $args, $default_args );

    $sql = $wpdb->prepare("SELECT {$wpdb->prefix}terms.term_id AS terms, 
                                   {$wpdb->prefix}terms.name AS name,
                                   {$wpdb->prefix}terms.slug AS name, 
                                   {$wpdb->prefix}term_taxonomy.taxonomy AS taxonomy,
                                   COUNT({$wpdb->prefix}posts.id) AS posts
                            FROM   {$wpdb->prefix}posts 
                                   INNER JOIN {$wpdb->prefix}term_relationships 
                                           ON ( {$wpdb->prefix}posts.id = {$wpdb->prefix}term_relationships.object_id ) 
                                   INNER JOIN {$wpdb->prefix}term_taxonomy 
                                           ON ( {$wpdb->prefix}term_relationships.term_taxonomy_id = 
                                                {$wpdb->prefix}term_taxonomy.term_taxonomy_id ) 
                                   INNER JOIN {$wpdb->prefix}terms 
                                           ON ( {$wpdb->prefix}term_taxonomy.term_id = {$wpdb->prefix}terms.term_id ) 
                                   INNER JOIN {$wpdb->postmeta} AS PM ON ({$wpdb->posts}.ID = PM.post_id)
                            WHERE  {$wpdb->prefix}term_taxonomy.taxonomy = \'%s\' 
                                   AND {$wpdb->prefix}posts.post_type = \'%s\'
                                   AND (PM.meta_key = \'%s\' AND PM.meta_value=\'%s\') 
                            GROUP BY terms
                            ORDER BY posts DESC",
                                array(
                                    $param[\'taxonomy\'],
                                    $param[\'post_type\'],
                                    $param[\'meta_key\'],
                                    $param[\'meta_value\'],
                                )
                            ) ;

    return $wpdb->get_results($sql);
}
使用此函数如下-

$terms = the_dramatist_get_terms_with_post_meta(
    array(
        \'meta_key\' => \'property_channel\',
        \'meta_value\' => 1,
        \'post_type\' => \'property\',
        \'taxonomy\' => \'property-city\'
    )
);
现在,您将以对象数组的形式获取数据。你可以做一个print_r 在那上面$terms 变量来理解函数返回数据的结构。运行afor 回路或aforeach 循环以组织数据。

希望这有帮助。

相关推荐

如何在Functions.php中链接style.css

我是WordPress的新手;我刚开始学习WordPress。我想把风格联系起来。函数中的css。php,但我无法解决这里可能存在的问题。谁能给我指出正确的方向吗?指数php<?php get_header(); ?> <?php if ( have_posts() ) { while ( have_posts() ) { the_post();