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
Blue Skies (insert all blue skies painting here)
Dark Red Skies (insert all dark red skies painting here)
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.
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!
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…
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;?>
Also, I tried this solution, but it doesn’t seems to work :
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.
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!
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!
© 2019 Advanced Custom Fields. Subscribe