如何让WP_QUERY返回帖子图片和类别?

时间:2015-07-25 作者:user1889580

我有一个边栏小部件,它将根据自定义元字段显示相关帖子。查询可以工作,但非常浪费。我所需要的只是文章标题、特色图片和文章类别。目前,下面的查询正在返回帖子的所有内容以及我不需要的大量数据。我不想通过从数据库中提取大量我不需要的数据来杀死我的服务器

$myquery = new WP_Query( "post_type=post&meta_key=nb_gameids_key&meta_value=14&order=ASC" );
为了更好地解释我想要的数据,我将使用一个sql查询来提供我想要的数据。我知道数据库的结构不是这样的,只是举个例子。

SELECT title,image,category FROM posts WHERE nb_gameids_key = 14 ORDER BY datecreated LIMIT 6

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

You don\'t need to worry to much about your query as there is not much you can do about it. You can unfortunately not just only query categies, post titles and images natively using WP_Query. Post categories needs to be called separately inside the loop, just like featured images.

If you dig deeper into core, you will see that all postdata and post terms are cached, so when you think you are making extra db calls, you are not. The cache is first checked for the requires data, if it is, the data is retrieved from the cache, otherwise the db is queried and the data is cached for later queries.

You can add \'no_found_rows\' => true, to your arguments if you are not going to paginate your query. This makes the query faster as it ignores pagination. Alternatively, you can use get_posts which has this already build in

Also, you can make use of transients to store the result from a query and only flush the transient when a new post is added in a particular category or custom field

结束

相关推荐

customize footer widgets area

我有一个自定义的页脚小部件区域,它在一行中水平显示最多4个小部件。如果我添加了4个以上的小部件,那么布局就会中断,因为我试图在同一行中显示它。我想让它更灵活,例如有2行(div),我可以在第一行中添加let’s 2 widget,在第二行中添加4个widget。可能我需要的是复制这一个,并制作两个页脚区域。这可能吗?如果可能,我如何实现?下面是我的小部件的实际代码。php: /* Footer Widgets */ $footer_widgets_num = wp_get_sidebars_