Support

Account

Home Forums General Issues Get Post Object ID when you know the content

Solved

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!).

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Get Post Object ID when you know the content’ is closed to new replies.