Support

Account

Home Forums General Issues Want Page ID in post_id Field

Solved

Want Page ID in post_id Field

  • Hi – How are you?

    First of all, many thanks for such a nice plugin and many many thanks to set it as free.

    Actually i ran into a problem and didn’t get the proper solution over internet that’s why posting this as a last Hope as i am using ACF plugin.

    What i want is explained below.

    I have 4 pages say A,B,C and D. I also have a custom post type say Products.

    Now what i want is to have an option in custom post type while adding the product to make that product featured on page A,B,C or D.

    So i have created the custom field with multiple select option and set 4 options that are A,B,C,D with page-ids as there values.

    Suppose i have a product with id 123 and assigned it to page A(page-id=10).
    So while displaying the page A, what should i exactly do to retrieve that post and displays that.

    Because what i have in database is like

    post_id = 123
    meta_key = test
    meta_value = a:1:{i:0;s:3:”10″;}

    where 10 is the page id.

    So i need a query or something , to show post_id 123 on page with id 10.

    According to my logic, i want the the record in database something like

    post_id = 10
    meta_key = test
    meta_value = a:1:{i:0;s:3:”123″;}

    So that when i am on page(id = 10), i will get the meta_value and then process a loop to get the product id.

    Please help.
    Thanks in advance.

  • Hi @ab_mannan

    I’m a little confused as to what you’re trying to accomplish.

    Correct me if I’m wrong:
    You have a multi-select field assigned to the custom post type named “Products”. Inside that multi-select field, there are numbers that correspond to page_id’s. What you want to do is display the products (that have that page_id assigned to it) when you are on that page,

    For example: When on (viewing) page_id=10, display all the products that have page_id=10 assigned to it.

    Is that what you’re trying to accomplish? Posting screenshots of your fields that correspond to this would actually help as well.

  • Thanks Mike for such quick response, wasn’t expecting 🙂

    I have mentioned details in the screenshot but let me tell you that you understand coorectly, that when user is on page_id=10, display all the products that have page_id=10 assigned to it.

    What i have explained in the screenshot is the logic that came in to my mind. Any suggestion on how to achieve my goal along with sample code, will be highly appreciated.

    Thanks a lot.

    Kind Regards,
    Abdul Mannan

  • @ab_mannan

    I think you’re making things a little more confusing than they are. Lets try simplifying the logic a bit.

    Post a screenshot of the actual custom fields inside the field group, please. That way we can get the correct field names.

    Start off by checking with a simple conditional if the current page_id is equal to your field.

    for example:

    
    <?php $field = get_field('your_field_name') ?>
    <?php if(get_the_ID() == $field): ?>
  • Hi Mike.

    Thanks a lot for simplifying the logic but i think i have multiple values in meta_value so how can i make a condition that if the page id appears in the meta_value then show that post here.

    Also, this is what i have currently in the field

    post_id = 116 (eg)
    meta_key = make_partner_featured
    meta_value = meta_value = a:1:{i:0;s:3:”mypageid”;}

    mypageid is what i have selected while adding a partner.

    I have attached 3 screenshots, 2 of field group and one for the page where i want it to appear. As i am not an expert in wordpress so it would be great to make a query to fetch that record or the piece of code that will fetch the required record.

    Thanks Mike, I really appreciate your help.

    Kind Regards,
    Abdul Mannan

  • Hi!

    Your logic is a bit backwards but it’ll still work.. So what you want is to display the products which are “connected” to a page being viewed.. unfortunately there’s no way to do this query on just those four pages (unless they use a custom template file) but you can do something like this on the page template:

    
    <?php
    $args = array(
    'post_type' => 'products',
    'posts_per_page' => -1,
    'meta_query' => array(
    		array(
    			'key' => 'make_partner_featured',
    			'value' => "$post->ID",
    			'compare' => '='
    		)
    	)
    );
    $products = new WP_Query($args);
    if($products->have_posts()): while($products->have_posts()): $products->the_post();
    
    //Show title
    the_title();
    
    endwhile; wp_reset_postdata(); endif;
    ?>
    

    Put this is your page template file and it’ll query your products when visiting a page and if the products ACF field value corresponds to the current pages id they will be fetched and the title of the product will be output.

    It’d be better to just do this when you know you’re on one of the pages in question and not all pages.. you can solve this by either giving them a custom page template or by a simple if statement around the code checking the current pages id against the four ids you have.. The second solution isn’t dynamic tho since you’d have to add another pages id in the if-statement if you want it to have the same feature..

  • Hi Jonathan. Thanks for the piece of code but it didn’t work.

    Don’t know why. This is what I have in database right now. Please see the screenshot.

    I have custom templates for all pages and ran your query in one of the template but didn’t work.

    As you can see in the screen shot, the meta value consists of the value 568 in serialized form.(which is actually the page id)

    So if i am on that page(568), will your query returns the post_id 5224 record ?

    Thanks.

  • Please let me know if i can handle this with any other way.

    I just want that if some one while adding the custom post (say product) selects the page ABC( id 568), that post will appear on that page. This is what i want. I have custom page templates for all pages also.

    Please help me out of this, if its possible with custom fields plugin.

    Thanks.

  • Hi Jonathan. I tweaked my self i bit and made it work.

    The code you posted above works well if i change the following thing

    ‘compare’ => ‘=’

    TO

    ‘compare’ => ‘LIKE’

    But will you please let me know, what difference does it make. Will it cause me trouble in future for any particular scenario ?

    Also
    ‘orderby’ => ‘meta_id’,
    ‘order’ => ‘DESC’

    is not working.

    Thanks for your help so far.

  • I can’t say 100% since I’m not that good at SQL.. but I think that LIKE means that it could be a string within a string.. It’s better that you google it.. “SQL LIKE”

    orderby meta_id does not work.. if you want to order them by their ID you can just set orderby => id ..

    default is date, but I’m guessing you’d want to order them by title?

    orderby => title ..

    anyway, you can find all orderby values here: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  • Thanks Jonathan. Your help really means to me.

    So, your previous code works for me by placing LIKE instead of =

    Everything is working now, but just for the sake of knowledge, can you let me know about any other approach to achieve what i want, so i can at least in future i will not implement that backward approach 🙂

    Kind Regards,
    Abdul Mannan

  • No worries dude, glad to help.

    Well i suppose this method is quite alright if you use a specific page template for those pages to make sure the query is not run on uhnecessery pages..

    One thing which would make it easier for you would be if you could have a select on the products which is automatically filled with the pages from that specific page template. This does not exist in ACF atm from what I know..

    IF you’re by any chance using ACF v3 I have created such a field which you can find here:
    https://github.com/jonathan-dejong/ACF-Pages-by-Template

    Unfortunately it doesnt work on v4 but you’re free to fork it and convert it to v4 if you like 🙂 Atleast you’ll have a lot of work done.

  • Perfect !!!!

    I will definitely tweak this next time if need such kind of thing again.

    The most active forum support I must say.Thanks all for your support so far.

    Kind Regards,
    Abdul Mannan

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

You must be logged in to reply to this topic.

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.