按自定义字段排序,即使为空 - wordpress

发布于 2024-10-26 01:52:39 字数 721 浏览 1 评论 0原文

我有一个帖子页面的查询。

它根据自定义帖子类型和自定义字段值返回结果。现在,我添加了根据另一个自定义字段对结果进行排序的功能。

$loop = new WP_Query( array ( 'post_type' => 'new', 'orderby' => 'meta_value_num', 'meta_key' => 'over-length', 'meta_query' => array( array( 'key' => 'over-make', 'value' => 'Doral', 'compare' => 'LIKE') ) ) );

我遇到了一点问题。我通过名为“超长”的自定义字段对结果进行排序,但似乎如果帖子不包含“超长”值,则会从结果中排除。

我想知道如何更改我的代码,以便它包含没有 orderby 值的帖子。

也只是想到了一个解决方法,但不知道该怎么做。我正在使用一个名为“更多字段”的插件来创建自定义字段。检查“超长”字段是否为空并将其设置为 0 会更容易吗?如果是这样我该怎么做呢?

更新

我已经进一步研究了这个问题。似乎如果没有为“超长”指定值,则自定义字段不会添加到数据库中的 wp_postmeta 中。如果我给帖子一个超长的值,然后返回并删除它,它实际上会将结果包含在我的查询中,因为该字段仍然存在于数据库中。那么,如果这个自定义字段有值或没有值,如何才能将其输入到数据库中呢?

I have a query for a page of posts.

It return results based on a custom post type, and custom field value. Now I've added the ability to order the results based on another custom field.

$loop = new WP_Query( array ( 'post_type' => 'new', 'orderby' => 'meta_value_num', 'meta_key' => 'over-length', 'meta_query' => array( array( 'key' => 'over-make', 'value' => 'Doral', 'compare' => 'LIKE') ) ) );

I've run into a bit of a problem. I'm ordering the results by a custom field called 'over-length' but it seems that if a post doesn't contain a value for 'over-length' it is excluded from the results.

I'm wondering how could I change my code so that it included post that don't have a value for orderby.

Also just thought of a workaround, but not sure how to do it. I'm using a plugin called "more fields" to create my custom fields. Would it be easier to check if the 'over-length' field is empty and set it to 0? if so how do I go about doing this.

Update

I've looked into the issue a bit further. It seems that if no value is given to 'over-length' the custom field is not added to wp_postmeta in the database. If I give a post a over-length value then go back and remove it it does in fact include the result in my query as the field still exist in the database. So how can I get this custom field to be entered into the database if it has a value or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩世 2024-11-02 01:52:39

关于您的最后一个请求 - 一种在所有帖子上添加meta_key(自定义字段)的方法,即使它留空:

我经常使用插件 自定义字段模板 用于此类事情。它与您正在使用的类似(更多字段),但它为您提供了多种使用字段的可能性,最重要的是对于这种情况,您可以调整隐藏字段和默认值。您也许可以设置 default=' '(空格),以便在留空时插入该字段。或者,做更多的工作,修改插件代码以将所有字段插入到数据库中,而不检查值是否为空。对于 CustomFieldsTemplate 来说,做到这一点应该不难。

About your last request - for a way to add a meta_key (custom field) on all posts even if it is left blank:

I often use the plugin Custom Fields Template for this kind of thing. It is similar to the one you are using (more fields) but it gives you many possibilities to play with the fields, and most importantly for this case, you can adjust hidden fields and default values. You can perhaps set a default=' ' (space) to have the field inserted if left blank. Or, with a bit more work, modify the plugin code to insert all the fields to DB without checking for empty the values. In the case of CustomFieldsTemplate it shouldn't be hard to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文