Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Wordpress:获取自定义帖子类型的类别,使用自定义分类法,按类别筛选_Database_Wordpress - Fatal编程技术网

Database Wordpress:获取自定义帖子类型的类别,使用自定义分类法,按类别筛选

Database Wordpress:获取自定义帖子类型的类别,使用自定义分类法,按类别筛选,database,wordpress,Database,Wordpress,我有一个非常严重的问题,希望这里有人能帮我解决。我有以下资料: “产品”的自定义帖子类型 “品牌”的定制分类法 &“类别”的标准分类法 每种“产品”都标有“品牌”,并归入父类和子类 我需要做以下工作: php页面显示所有包含“产品”的“类别”,这些类别具有“品牌”分类法,其值为“品牌”的页面名称由于@MikeSchinkel,此功能与以下功能配合良好 $term = get_query_var('term'); $brand = get_term_by('slug',$term,'bra

我有一个非常严重的问题,希望这里有人能帮我解决。我有以下资料:

“产品”的自定义帖子类型

“品牌”的定制分类法

&“类别”的标准分类法

每种“产品”都标有“品牌”,并归入父类和子类

我需要做以下工作:

php页面显示所有包含“产品”的“类别”,这些类别具有“品牌”分类法,其值为“品牌”的页面名称由于@MikeSchinkel,此功能与以下功能配合良好

$term = get_query_var('term');
    $brand = get_term_by('slug',$term,'brands'); // This here just to illustrate
    $categories = get_cross_referenced_terms(array(
    'post_type'        => 'Products',
    'taxonomy'         => 'category',
    'related_taxonomy' => 'brands',
    'term_id'          => $brand->term_id
  ));
它使用以下功能:

// query to get categories for a specific tag
function get_cross_referenced_terms($args) {
  global $wpdb;
  $args = wp_parse_args($args,array(
    'post_type'        => 'Products',
    'taxonomy'         => 'category',
    'related_taxonomy' => 'brands',
    'term_id'          => 0,
  ));
  extract($args);
  $sql = <<<SQL
SELECT DISTINCT
  {$wpdb->terms}.*,
  COUNT(*) AS post_count
FROM
  {$wpdb->terms}
  INNER JOIN {$wpdb->term_taxonomy} ON {$wpdb->terms}.term_id={$wpdb->term_taxonomy}.term_id
  INNER JOIN {$wpdb->term_relationships} ON {$wpdb->term_taxonomy}.term_taxonomy_id={$wpdb->term_relationships}.term_taxonomy_id
  INNER JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id={$wpdb->posts}.ID
  INNER JOIN {$wpdb->term_relationships} related_relationship ON {$wpdb->posts}.ID=related_relationship.object_id
  INNER JOIN {$wpdb->term_taxonomy} related_term_taxonomy ON related_relationship.term_taxonomy_id=related_term_taxonomy.term_taxonomy_id
  INNER JOIN {$wpdb->terms} related_terms ON related_term_taxonomy.term_id=related_terms.term_id
WHERE 1=1
  AND related_terms.term_id<>{$wpdb->terms}.term_id
  AND {$wpdb->posts}.post_type='%s'
  AND {$wpdb->term_taxonomy}.taxonomy='%s'
  AND related_term_taxonomy.taxonomy='%s'
  AND related_terms.term_id=%d
  AND {$wpdb->term_taxonomy}.parent=0
GROUP BY
  {$wpdb->terms}.term_id
SQL;
  $sql = $wpdb->prepare($sql,$post_type,$taxonomy,$related_taxonomy,$term_id);
  $terms = $wpdb->get_results($sql);
  return $terms;
}
我想这可能是我可以在这里的数据库查询中添加的内容:

AND {$wpdb->term_taxonomy}.parent=0
但这有点超出我的能力范围!如果有人能帮我,我将非常感激

谢谢


戴夫

是的,你很接近。这可能会奏效:

AND {$wpdb->term_taxonomy}.parent=[$term_id}
如果您只想要传递的品牌的子品牌,否则如果需要单独传递,可以在提供的函数中添加参数

例如:

    'term_id'          => 0,
    'parent_id'          => 0,
));
然后在下面你会使用这个新变量

AND {$wpdb->term_taxonomy}.parent=[$parent_id}
AND {$wpdb->term_taxonomy}.parent=[$parent_id}