Home › Forums › General Issues › Get Post Object ID when you know the content
Evening all – long time ACF user who has given up searching and hopes someone can help him!
Quick version:
We have Manufacturers (Custom Post Type)
We have Products (Custom Post Type)
When you add a Product, you select the name of the Manufacturer from a drop down list, populated from the Manufacturers CPT list. So far, so good.
I want to run a query on a Manufacturer specific page, show all Product connected to this Manufacturer.
This query (below) works – but only because I am specifying the meta_value.
207 is the Post Object which matches the manufacturers name (field name ‘product_manufacturer’). And the page the query is running on is titled the same.
So, I want some way – by creating a variable perhaps? – to ‘look up’ the ID (and get 207) knowing that the_title matches ‘product_manufacturer’.
This gives me a variable based on the page title
<?php $pagemanuf = get_the_title(); ?>
But if I replace 207 below with $pagemanuf everything goes belly up.
I know this is simple, and I know I’ll kick myself when someone tells me what I’ve done wrong!
<?php $args = array(
‘post_type’=>’products’,
‘orderby’=>’rand’,
‘posts_per_page’=>’-1′,
‘meta_key’ => ‘product_manufacturer’,
‘meta_value’ => ‘207’
);
$manufprods=new WP_Query($args);
while ($manufprods->have_posts()) : $manufprods->the_post();?>
<?php get_template_part(‘inc/productloop’); ?>
<?php endwhile; wp_reset_postdata();?>
Thanks in anticipation, and an offer of beer if the lucky helper is close to Manchester, UK!
Thanks,
Ross
you need the ID and not the title, ACF stores only the ID so when you are showing a manufacturer page you should be able to do
'meta_value' => $post-ID
John, you’re a star. I knew it would be something stupidly obvious. My brain had obviously decided to go on a short holiday yesterday.
I’m guessing (often dangerous), that you’re not in close vicinity to Manchester – but should you ever find yourself in this neck of the woods, let me know and I’ll be buying you a pint of something cold and refreshing.
Heh, I just wish I could spot the obvious errors in my own code the way I can spot them in others’. It’s amazing how long you can look at a line of code without spotting the simple things 🙂
Tell me about it. I think when you’re so close to something – especially when your brain has taken you down a specific path – you don’t always realise what you’ve done (which is usually – reverse and check you set off in the right direction in the first place!).
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🤔 Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee you’re represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.