Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Could you post an example of one of the queries you run for your own site, so I can look into this a bit more?

    If you’d rather not post about your site here in the forums, you can also contact us via thhis contact form:
    https://developer.wordpress.com/contact/

    Thanks!

    Thread Starter Marco Canestrari

    (@marcocanestrari)

    No problem to post here.

    My site is badtaste.it, Jetpack active and connected. Related posts are working well and correctly filtered using the hook ‘jetpack_relatedposts_filter_date_range’.
    I now need to get the related posts via API, to put them in my AMP version of the pages.
    This is an example of the code I’m using: there is response but not filtered. I’d like to get posts old not more then 1 year.

    Thank you!

    $query_json = '{
            "size" : "3",
            "query": {
                    "filtered": {
                            "filter": {
                                    "range": {
                                            "created": {
                                                    "gte": "now-1y"
                                            }
                                    }
                            }
                    }
            }
    }';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,'https://public-api.wordpress.com/rest/v1/sites/www.badtaste.ut/posts/150840/related');
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$query_json);
    $results_related = curl_exec($ch);
    curl_close($ch);
    $response = json_decode($results_related);
    Thread Starter Marco Canestrari

    (@marcocanestrari)

    Note: I tried both “date” and “created” criteria, none of them works, unfortunatly

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    That’s weird indeed. I’ve asked one of our Related Posts experts to take a look, and I’ll get back to you as soon as we figure this out!

    In the meantime, you should be able to use our Raw class to get related posts with all the filters you already use on your site.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Could you try to use date_gmt instead of created? That should do the trick.

    Thread Starter Marco Canestrari

    (@marcocanestrari)

    Unfortunatly not ??
    I’ll try the raw class asap and tell you if it works!

    Thread Starter Marco Canestrari

    (@marcocanestrari)

    raw class worked well! Thank you!

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    Unfortunatly not ??

    That’s weird. date_gmt seems to work in my case:

    curl -XPOST https://public-api.wordpress.com/rest/v1/sites/71539853/posts/150840/related -d '{ "size":1, "fields":["post_id"], "filter":{"range":{"date_gmt":{"gte":"now-1y"}}} }'

    Either way, I’m glad to hear the raw class worked well.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Date range filter not working with related REST API’ is closed to new replies.