Support

Account

Home Forums General Issues Best way to use ACF in Artist/Artworks relationship

Solving

Best way to use ACF in Artist/Artworks relationship

  • Hi,

    I’m not an expert coder, but i used ACF quite a lot in the past, i’m kind of hitting a wall on a project right now. I would like to know what would be the best way to achieve this :

    I have a CPT ARTWORKS (i use a CPT to produce an art catalog, a bit like woocommerce product CPT)
    I have a CPT ARTISTS (here are displayed the ARTWORKS on the ARTIST page)

    I have a relationship between both, and use ACF Post to Post plugin to have a bidirectional update: i assign an artwork to an artist. So far all is good!

    Where i’m stumbling is here:

    Each artist can produce different series of work…

    Let say :

    Peter Griffin, Painter Extraordinaire

    Series :

    Blue Skies (insert all blue skies painting here)
    Dark Red Skies (insert all dark red skies painting here)
    Etc..

    I can’t use a Repeater field with Post to Post plugin…the relationship field needs to be a top level…some artists have 15 series…

    How would you guys tackle this with ACF? How could i subdivide all the artworks in series, if need be (some artist do not have series)?

  • If I needed to do something like this a couple of options come to mind.

    One option would be to use a hierarchical post type for the work. The parent post would be used for the “Series” and then child posts would be used for each item in the series. The you relate the artist to the series.

    Another option would be to use a 3rd post type for the series. You relate the artist post type to the series and you relate each item to the series.

    A third option would be to use a custom taxonomy for the series.

    Another options would be to build the custom code needed to create a bidirectional field for this specific use that would work in a repeater, but this one would be my last choice.

    Each of the above have their pros and cons and I would choose base on the end result I needed. How do these things need to be shown on the site? Do they need to be searchable? How? etc… The second option probably would give the most flexibility.

  • Hi John!

    Thank you very much for your detailed suggestions. I’m thinking the same as you, as to create a Series CPT would be the more elegant and flexible solution.

    Tricky part with this project, is that we have a searchable catalog (probably going to use Facet WP for that), so exactly like a woocommerce site (series wont matter there), BUT on the artist page itself, client wants the works to be displayed in a slideshow AND divided by series, instead of linking to the actual product page.

    Anyways, i’ll fiddle with the Series CPT today and see what i’m coming up with.

    Thanks again for taking the time to answer me.

  • In I might actually make the series post a child of the artist post.

    On the artwork post I would have select field for artist. I would hand code the bidirectionality of this field with a field on the artist page.

    I would have a second field that is dynamically populated with the series based on the artist selected. This would likely be a select field using something similar to this https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/dynamic-select-example.

    Then you can do a meta query to get posts from works that match both the artist and their series. Using series as a child of artist you can also get all of the series by an artist by getting child pages or you can easily get the artist for any series by getting the parent post.

  • OK, i’ll look into that. Might have some questions later.

    Thanks and keep you posted!

  • Hi John,

    I manage to get all things working, i stuck with the 3 CPT approach. Thanks again for your tips.

    I have one more question :

    In the WordPress admin, in the Relationship Field of an item, you can drag and drop post on in certain order…

    How can i display my post exactly in the same order as they are in the admin? I tried all the ‘orderby’ => ” in my query, but none seems to work…

    Thanks!

  • Here’s the code i’m using.

    
    <?php 
    
    						$series = get_field('serie_artist');
    
    						?>
    						<?php if( $series ): ?>
    							<?php foreach( $series as $serie ): ?>
    							
    								<h2><?php echo get_the_title( $serie->ID ); ?></h2>
    								
    								<?php
    									$oeuvres = get_posts(array(
    										'post_type' => 'artworks',
    										'post_status' => 'publish',
    										'meta_query' => array(
    											array(
    												'key' => 'art_serie',
    												'value' => $serie->ID,
    												'compare' => 'LIKE'
    											)
    										)
    									));
    								?>
    									<?php foreach( $oeuvres as $oeuvre ): ?>
    									
    									        <h3><?php echo get_the_title( $oeuvre->ID );  ?></h3>
    									<?php endforeach;?>
    							<?php endforeach; ?>
    							
    						
    													<?php else :
    														echo "No Works!"; ?>
    													<?php endif; ?>
    

    It’s basically this part i’m trying to get in the right order :

    	<?php foreach( $oeuvres as $oeuvre ): ?>
    									
    									        <h3><?php echo get_the_title( $oeuvre->ID );  ?></h3>
    									<?php endforeach;?>
  • If you get the value of a relationship field they should already be ordered in the order they appear in the field. No extra code should be required.

  • John, you are right. Since im getting the value of the SERIES post type relationship field, they are indeed classified as in the admin : $series = get_field(‘serie_artist’);

    But, for the ARTWORKS, that are listed under each SERIES, i guess it’s not respecting the order, because i’m doing a get_post :

    $oeuvres = get_posts(array(
    										'post_type' => 'artworks',
    										'post_status' => 'publish',
    										'meta_query' => array(
    											array(
    												'key' => 'art_serie',
    												'value' => $serie->ID,
    												'compare' => 'LIKE'
    											)
    										)
    									));

    Do you see a solution to get the Artworks respect the order of the relationship field? Or is it not feasible?

    I hope i’m clear enough…

  • Is artwork a relationship on the series post?

    If it is then just get that field from the series instead of doing a query.

    
    get_field('works', $serie->ID);
    
  • OMG thank you! The solution was so simple…it works great now, i can reorder both the series and the artworks.

    Do you have a Paypal address? I’m buying you a beer!

  • Hi John!

    We are running into another problem…

    We are using WPML. Everything works fine on the default language (English). But when i try to show the artist related works on the secondary language (French) nothing is shown..

    The var_dump of : $arts = get_field(‘art_artist’); where “art_artist” is my relationship field return a value in the default language, but returns NULL in the secondary language.

    All the Artworks are translated (duplicated).

    Hope you can help me once again!

    Thanks!

  • OK, i guess that $arts = get_field(‘art_artist’); is actually looking at the artist page ID of the original language…

    If i create a custom Query, i can get the Artworks to be listed in French and English :

    I get the actual artist page ID :

    
    if (ICL_LANGUAGE_CODE == 'en'):
        $id_art = get_the_ID();
    
    elseif (ICL_LANGUAGE_CODE == 'fr'):
        $id_art = get_the_ID();
    endif;
    

    Then :

    $args = array(
    				'post_type' => 'artworks',
    				'posts_per_page' => -1,
    				'meta_query' => array(
    					array(
    						'key' => 'art_artist', 
    						'value' =>  $id_art,
    						'compare' => 'LIKE'
    						)
    					)
    				);	
    

    The problem now is that the post listed do not respect the post order that was assigned in the admin…like I mentioned in my reply of June 12, 2019 at 2:38 am.

    Would you have a better solution?

    Thanks in advance!

  • PLEASE IGNORE MY QUESTIONS ABOVE…

    After 5 hours of banging my head…i found that i had some custom fields that were not translated and also not set to copy.

    Gotta love coding / wordpress sometimes.

    BTW John i still owe you a beer, please get me a way (Paypal, etc) to send you something for the previous help you gave me. Thanks!

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

The topic ‘Best way to use ACF in Artist/Artworks relationship’ is closed to new replies.