【问题标题】:WP: mysql query: add posts to a category that are in multiple categoriesWP:mysql查询:将帖子添加到多个类别中的类别
【发布时间】:2021-08-10 07:20:04
【问题描述】:

我找到了一个用于将帖子添加到另一个单一类别中的类别的 sn-p:


# remember to replace @CategoryID and @OldCategoryID.   @OldCategoryID is in the middle
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) 
  SELECT object_id, @CategoryID, 0 FROM wp_term_relationships tr2 
    WHERE tr2.term_taxonomy_id = @OldCategoryID AND object_id NOT IN 
      (SELECT object_id FROM wp_term_relationships tr3
        WHERE tr3.term_taxonomy_id = @CategoryID);

但是我无法让它适用于一系列类别的帖子。

所以这不起作用:


INSERT INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) 
  SELECT object_id, 101, 0 FROM wp_term_relationships tr2 
    WHERE tr2.term_taxonomy_id IN (61,62,63,64,65) AND object_id NOT IN 
      (SELECT object_id FROM wp_term_relationships tr3
        WHERE tr3.term_taxonomy_id = 101);

感谢帮助

【问题讨论】:

    标签: php mysql wordpress categories


    【解决方案1】:

    为什么不使用 wordpress 内置函数 wp_insert_post()

    来自 wp 的示例:

    // Create post object
    $my_post = array(
      'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
      'post_content'  => $_POST['post_content'],
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_category' => array( 8,39 )
    );
     
    // Insert the post into the database
    wp_insert_post( $my_post );
    

    【讨论】:

    • 我正在对超过 50,000 个帖子进行查询。用wordpress codex会太慢
    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2019-10-24
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多