基于ACF属性的REST过滤器不起作用

时间:2018-08-15 08:13:49

标签: wordpress advanced-custom-fields wordpress-rest-api

我有此REST数据:

class PriceList(models.Model):
    class Meta:
        verbose_name = _("ценоразпис")
        verbose_name_plural = _("ценоразписи")


    name = models.CharField(blank=False, null=False, max_length=300, verbose_name="наименование")
    description = models.CharField(blank=False, null=False, max_length=300, verbose_name="Описание")

    def show_name(self):
        return '{}'.format(self.name)
    def show_description(self):
        return '{}'.format(self.description)
    def __str__(self):
        return '{}'.format(self.name)

class PriceListItem(models.Model):
    class Meta:
        verbose_name = _("елемент от ценоразпис")
        verbose_name_plural = _("елементи от ценоразпис")
        ordering = ['id']

    price_list = models.ForeignKey(PriceList, blank=False, null=False, verbose_name="ценоразпис", on_delete=models.CASCADE)
    title_bg = models.CharField(blank=False, null=False, max_length=200, verbose_name="наименование BG")
    description_bg = models.CharField(blank=False, null=False, max_length=200, verbose_name="Описание BG")
    title_en = models.CharField(blank=False, null=False, max_length=200, verbose_name="наименование EN")
    description_en = models.CharField(blank=False, null=False, max_length=200, verbose_name="Описание EN")
    price_bg = models.DecimalField(blank=False, null=False, decimal_places=PRICE_DECIMAL_PLACES, max_digits=PRICE_DECIMAL_DIGITS, verbose_name="BGN")
    price_en = models.DecimalField(blank=False, null=False, decimal_places=PRICE_DECIMAL_PLACES, max_digits=PRICE_DECIMAL_DIGITS, verbose_name="EUR")

    def __str__(self):
        return '{}'.format(self.id)

我创建了一个过滤器来查询“ myid”参数,如下所示:

from django.contrib import admin

from pricing.models import PriceList, PriceListItem


class PriceListItemInline(admin.TabularInline):
    model = PriceListItem


class PriceListAdmin(admin.ModelAdmin):
    model = PriceList
    inlines = [PriceListItemInline, ]
    list_display = ('name', 'description')




admin.site.register(PriceList , PriceListAdmin)

这是我添加到 functions.php 中的过滤器代码:

[{"id":215,"acf":{"stad":{"value":"barcelona","label":"barcelona"},"description":"","images":[{"ID":191,"id":191,"title":"logo-black.png","filename":"logo-black.png","filesize":3080,"url":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","link":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/logo-black-png\/","alt":"","author":"1","description":"","caption":"","name":"logo-black-png","status":"inherit","uploaded_to":0,"date":"2018-04-16 15:39:37","modified":"2018-08-05 15:19:12","menu_order":0,"mime_type":"image\/png","type":"image","subtype":"png","icon":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-includes\/images\/media\/default.png","width":443,"height":98,"sizes":{"thumbnail":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-150x98.png","thumbnail-width":150,"thumbnail-height":98,"medium":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black-300x66.png","medium-width":300,"medium-height":66,"medium_large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","medium_large-width":443,"medium_large-height":98,"large":"https:\/\/wordpress-132670-574369.cloudwaysapps.com\/wp-content\/uploads\/logo-black.png","large-width":443,"large-height":98}}]}},{"id":205,"acf":{"stad":{"value":"oslo","label":"oslo"},"description":"","images":false,"myid":"333"}}]

酒店是自定义帖子类型。

查询始终返回所有行,过滤器无效。

这是怎么了?

2 个答案:

答案 0 :(得分:1)

如果仔细查看过滤器,您将了解错误。

apply_filters(“ rest _ {$ this-> post_type} _query”,数组$ args,WP_REST_Request $ request);

这是您的代码:

Int

在这里rest_hotels_query:我们需要输入帖子类型名称,请小心,如果您的帖子类型名称是hotel,则过滤器应类似于:“ rest_hotel_query”

这是工作代码:

@IBAction func guessPressedButton(_ sender: Any) {
    let randomNumber = arc4random_uniform(11)
    if let validNumber = Int(textField.text!), validNumber == randomNumber {
        resultLabel.text = "التخمين صحيح"
    }
}

与posts控制器的collection参数相同的情况:

apply_filters(“ rest _ {$ this-> post_type} _query”,数组$ args,WP_REST_Request $ request);

应该是:

add_filter('rest_hotels_query', function($args, $request){

$myid = $request->get_param('myid');

if (!empty( $myid)) 
{
    $args['meta_query'] = array(
        array(
            'key'     => 'myid',
            'value'   => $myid,
            'compare' => '=',
        )
    );      
}

return $args;

}, 10, 2 );

答案 1 :(得分:0)

rest_query_vars过滤器不再存在。看看https://developer.wordpress.org/reference/hooks/rest_this-post_type_collection_params/https://developer.wordpress.org/reference/hooks/rest_this-post_type_query/

所以替换掉这个

add_filter('rest_hotels_vars', function ($valid_vars)
{
   return array_merge($valid_vars, array('myid', 'meta_query'));
});

与此

add_filter('rest_hotels_collection_params', function ($valid_vars)
{
   return array_merge($valid_vars, array('myid', 'meta_query'));
});

它应该可以工作。