【问题标题】:How to select all posts in Wordpress that belong to a parent category如何在 Wordpress 中选择属于父类别的所有帖子
【发布时间】:2018-01-05 08:37:45
【问题描述】:

我正在管理一家在 Wordpress 上使用 WooCommerce 插件的商店。

它们的类别配置如下:

第 1 类
- 1A类
-- 1B 类
--- 1C类

猫 2
- 2A类
-- 猫 2B
--- 2C 类

如您所见,类别是嵌套的。

产品(帖子)属于类别层次结构的底层。

我想要的是一个选择查询,例如,它返回属于“Cat 2”的所有产品。

如何做到这一点?

谢谢你, 克雷格

【问题讨论】:

  • 即使 Wordpress 可能有一个标准的表格架构,您仍然应该在此处发布它以完成您的问题。

标签: php mysql wordpress woocommerce


【解决方案1】:
$args = array(
      'post_type' => 'post', 
      'orderby' => 'ID', 
     /* 'meta_key' => 'rankk', */
      'order' => 'DESC', 
      'posts_per_page' => 100,
      'tax_query' => array(
            array(
                'taxonomy' => 'category',
                'field'    => 'term_id',
                'terms'    => 'YOUR PARENT CATEGORY ID',
            ),
        ),
);

$q = new WP_Query( $args );

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
        $q->the_post();
        the_title();
    }
    wp_reset_postdata();
} 

// 输入 post_type 和父类别 ID(Cat2) 然后这段代码将给出你想要的。

【讨论】:

  • 谢谢贾斯比尔。抱歉,如果我不清楚,但我希望能够在 SQL 查询中选择它。我需要能够更新此父类别中产品的价格,其中有 1000 个!!
  • @cs10 使用查询回显$q->request获取sql
【解决方案2】:

我找到了一种在 SQL 语句中执行我需要的方法。

以防万一这可能对任何人有所帮助,这里是选择查询。

您需要找到您希望搜索的类别的 term_taxonomy_id,在我的例子中,Cat 2 的 ID 为 8412。

SELECT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_term_relationships.object_id, wp_term_relationships.term_taxonomy_id
from wp_postmeta
INNER JOIN wp_term_relationships ON wp_postmeta.post_id=wp_term_relationships.object_id
where term_taxonomy_id = '8412'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多