Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pass post id to Facebook's pixel code WordPress


Hi! I'm implementing Facebook dynamic product ads. When you add the ViewContent pixel to a product page, you need to insert that product’s specific ID manually or via a tag from the ecommerce platform. I'm having trouble getting that tag to work in my code. Specifically, I need to insert woocommerce (or wordpress) post id in the content_ids part. Any ideas?
<!-- Facebook Pixel Code --> <script> !function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod? n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n; n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0; t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window, document,'script','//');
fbq('init', 'xxx'); fbq('track', 'PageView');
fbq('track', 'ViewContent',{ content_ids: 'post id goes here', <====== content_type: 'product'
</script> <noscript><img height="1" width="1" style="display:none" src="" /></noscript> <!-- End Facebook Pixel Code -->

Answers (2)


Reigel Gallarde answers:

replace this

'post id goes here'


'<?php echo $post->ID; ?>'

bubiduk comments:

Thanks. Tried that already. The result is the pixel returning the php code as it is:

content_ids: <?php echo $post->ID; ?>

Reigel Gallarde comments:

where are you putting this script? php file?

bubiduk comments:

In the header.
Just wanted to add this. In the past I had succesfully implemented something somewhat similar.
Look at this See how they grab the order price and insert it in the script.
Now I need something like this but for the specific case stated in the original question.


Andrea P answers:

if you are placing that code into a .js file or anywhere where you can't actually use php to retrieve the post id, you could try with this (dirty) hack.

put this code in your header. be sure it's below the wp_head();

function AP_get_id(){
var postID = <?php echo $post->ID; ?>;
return postID;

then in the other file, instead of
'post id goes here'



without quotes.

not sure if it would work on your scenario, but I think it worth it to try. ;)

bubiduk comments:

Did not work. Trying this stopped the pixel from loading correctly.

Andrea P comments:

if you have to put this code in the header, then the Reigel code should've worked. if it didn't might be because you've placed it before the function

that code must be placed after that function, otherwise the $post global won't be ready.

bubiduk comments:

you are right, it works now. I was using the "custom code" thingy from the theme, now I tried directly in the header and made sure of the location.
Now, how do we do this? Do I award the money to Reigel as he provided the first solution or to Andra who specified where to put it?

Andrea P comments:

Riegel would have said the same if he was online when I posted my last reply.
give the prize to him ;)

bubiduk comments:

cool! Just voted, Thanks!!