【问题标题】:Is there a way to display my query results as posts with images (Wordpress site)?有没有办法将我的查询结果显示为带有图像的帖子(Wordpress 网站)?
【发布时间】:2019-02-14 08:51:30
【问题描述】:

这是一个查询示例:

  $posts = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'product'");

这将为我们提供所需的 ID。

[ID] => 2
[ID] => 6
[ID] => 25 and so on..

现在我想显示具有这些 ID 的帖子。

【问题讨论】:

  • 可以分享一下表结构吗?
  • 我忘了说明这是一个 Wordpress 网站,表格是 wp_posts(ID(pk)、帖子标题、post_type、post_parent、post_name、post_status、guid)

标签: mysql sql wordpress


【解决方案1】:

您可以简单地更改查询以返回 post_name 以及 id,例如:

SELECT ID, post_name FROM $wpdb->posts WHERE post_type = 'product';

如果您想显示帖子内容并且它存在于另一个表格中,那么您需要使用JOIN

【讨论】:

    【解决方案2】:

    通过 ID 获取帖子对象:

    $post_7 = get_post( 7 ); 
    $title = $post_7->post_title;
    

    和数组

    $post_7 = get_post( 7, ARRAY_A );
    $title = $post_7['post_title'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      相关资源
      最近更新 更多