0

I am trying to get_field() using post id in one signal filter, I am getting the value of post id. But for some reason note getting the values of get_field(). I have imported wp-load, but still didn't get the values. Can someone help me regarding this ?

require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);

function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
//This works for already published posts
//$current_affairs = get_field('current_affairs','3210'); 
//Below does not work
$current_affairs = get_field('current_affairs',$post->ID);
$title = $current_affairs[0]['headline'];
$image = $current_affairs[0]['image'];
$fields['isAndroid'] = true;
$fields['isIos'] = true;
$fields['isAnyWeb'] = false;
$fields['isChrome'] = false;
$fields['data'] = array(
    "headline"=> $title,
    "image"=> $image,
    "type" => "news",
    "news_data" => $current_affairs
);
/* Unset the URL to prevent opening the browser when the notification is clicked */
unset($fields['url']);
return $fields;
}
11
  • please check your $current_affairs value by var_dump($current_affairs); Sep 28, 2018 at 9:46
  • Yes I checked, it has values. I made another test page and tried fetching data, it worked. But when I am trying to fetch in functions.php, it is not working
    – topper1309
    Sep 28, 2018 at 9:51
  • 1
    I think this is because you're doing it too early. You need to wrap your code in the necessary hook so that it occurs at the correct time. Sep 28, 2018 at 9:55
  • try using get_post_meta() function instead of get_field() Sep 28, 2018 at 10:04
  • get_post_meta() didn't work @KrishnaJoshi
    – topper1309
    Sep 28, 2018 at 10:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.