Support

Account

Home Forums General Issues Link product to post (and vice versa)

Solved

Link product to post (and vice versa)

  • I have a custom post type called Projects
    I created a Related Products field for the Projects post type, where I select Products featured in that Project
    Image
    I then display the products on the front end like so:

    $products = get_field('related_products');
    
    if( $products ) {
    	$products_string = '';
    	foreach( $products as $p ) {
    	    	$products_string .= $p->ID.',';
    	    }
    	$products_string = rtrim($products_string,',');
    	echo do_shortcode('[title style="bold-center" text="אז מה עשינו?"]');
    	echo do_shortcode('[ux_products ids="'.$products_string.'"]');
    }

    (I get the product IDs, then place them in a slider shortcode)

    How do I link the Products and Projects together, so that if Product X is featured in Project X, display the Project on the Product page (i.e. how do I get the Project IDs where the product is featured)

  • You can try this https://www.advancedcustomfields.com/resources/querying-relationship-fields/

    or you could try implementing this https://www.advancedcustomfields.com/resources/bidirectional-relationships/

    or you could try out a plugin like https://wordpress.org/plugins/post-2-post-for-acf/

    The last 2 will not update the relationships automatically. You’d need to edit all the posts on one side or the other. There are a lot of topics here on the forums about the first option, but I generally use the last one.

  • The second one worked great for me, thanks a lot!

  • I noticed I’m unable to remove/deselect posts from the relationship
    e.g. I select 2 related posts, then update the post being edited, and the post shows in the two other posts
    I then deselect the 2 related posts and update, and they won’t be removed from the relationship
    How do I go around it?

  • There are 2 parts of the function at. The first part of the function adds values and the second part deletes them. To be honest, I’m not 100% sure how the develops example does the work, I’ve never looked at it closely. My plugin does things quite a bit differently.

    I have another example here that does it in yet another way https://github.com/Hube2/acf-filters-and-functions/blob/master/acf-reciprocal-relationship.php

  • I’d like to give it a try with the plugin (post-2-post), just unsure what the setup would be and how I would pull the values
    Here is my current setup:

    Relationship Field

    Product

    Portfolio

    The two post types are Product (‘product’) and Portfolio (‘featured_item’)

    What would I need to change in my original code to pull the related post IDs using the plugin?

  • The setup would be to install and activate the plugin. You are using the same field group and therefore the same field with the same name on both post types. My plugin will automatically do the work of creating the two way relationship on this field without any additional work.

  • Ok, that worked! I seem to be able to add relationships, but I can’t remove them, e.g. I select project 1 and project2 in the product1 relationship field, and save it; it displays on front end as expected, but when I remove project1 and project 2 from the selection and then update the post (product1), they just stay there; what could possibly cause this?

  • I don’t know. I just did a test of the plugin and relationships are being added and removed as expected for me. It may be a conflict with something else, which could have also been causing the issue with your other attempt using the ACF example code.

    You’ll need to deactivate plugins and maybe switch themes to narrow down what might be causing the conflict

  • Yes I think because I’m unable to remove posts from the relationship field itself, I think it is more of a conflict in the database
    How do I clear the database from everything related to these relationships?
    The field names I used/switched between, since the first solution I attempted were related_products, related_projects, related_posts, relatedposts

  • Just to note, when the relationship field is empty it also reflects on front end
    e.g in product2 I haven’t addded relationships yet so the page renders “no related posts” message
    (just as expected)
    The thing is it won’t can’t clear/update the relationship field once there are existing relationships as if something prevents it

  • Values in the database, if you’ve renamed the fields, should not be conflicting with the new fields. I can’t say what the conflict is to be honest. It could be anything from some filter that’s been created to duplicated field names. The only way to clear out the old data for old field names is to delete them manually from the database.

  • Yes that’s what I possibly did, I created duplicate fields, then deleted them, created new fields with the same names etc, I think that’s what caused the conflict
    How can I clear this old data from the databse or where are they located in the database?

  • All of the values saved by acf for posts are in the _postmeta table.

  • Ok I was able to remove those values from the database, that cleared up the relationships; though still unable to remove/update selections from the edit page

    I think I’ll try something different, what would be the setup if I want to seperate the post types so that in Product page it will let you select only Portfolio posts and vice versa (Portfolio page lets you select only Product posts)

    Do I create two field groups, Related Products and Related Projects, then create a relationship field ‘related_posts’ in both groups? Or is it going to cause a conflict?

  • Yes, you could do that, but it shouldn’t be necessary.

  • Ok, what would be the appropriate method?

  • the only requirement for the plugin is that the field names are the same

  • Ok then I got it working that way, just unsure why I’m unable to update/remove relationships once published, I’ll start a new thread on the issue

  • As I said, it’s working for me. Have you tried removing other plugins or switching themes to see what might be interfering?

  • Here’s what I just tried
    1. disabled all plugins except ACF
    2. changed theme to Twenty Seventeen
    3. create new product
    4. create new project
    5. create new relationship field ‘test_field’ showing on product and project post types
    6. linked new product to new project and published, relationship value is saved (on product edit page)
    7. deselected the value and published; value not saved
    I don’t think it’s a bug in the plugin but I tried everything, what else could it be?

  • I just noticed values do update in ‘featured_item’ posts but not in ‘product’ posts
    I’m still unable to update/remove relationship values in a ‘product’ post

  • Ok I resolved the issue with saving values by disabling Yith Tab Manager
    The last issue I have is with bidirectional relationship is between two different post types (product and featured_item) using two different field names (related_products and related_projects)
    I was able to create bidirectional fields when I used a single field related_posts which I placed in both the Product and Project edit pages, but I’ll need to use a different field name for each post type
    Is it possible to create bidirectional fields using two different field names?

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

The topic ‘Link product to post (and vice versa)’ is closed to new replies.