Home › Forums › General Issues › Post object with conditions performance, elsif or switch or separate blocks › Reply To: Post object with conditions performance, elsif or switch or separate blocks
I’m a little confused by your question.
You said that there would be 20+ post objects.
Do you mean that you will have 20+ lines of code that looks like this
$post_object = get_field('post_object_categoryone_1','options'); // Set field to grab
or that the above post object field can contain 20 plus posts?
If you will have 20+ calls to ACF to get 20+ post object fields then this would cause a performance issue due to the number of DB queries that will need to be done. I would look at ways to avoid this. For example a relationship field which allows posts in the relationship to be put in the order that they should be shown so that there is only a single query to get all of the posts.
However, if you will only have the 2 post object fields that will contain these 20+ posts then this would not be an issue because each field only creates a single query.
Beyond that it depends on what you are doing once you have all of the posts, there is no indication of what you’ll be doing in each case based on the conditions. Remember that in some cases every call to get_field()
and other ACF function may result in additional DB queries depending on if the meta value are cached, and if you get a post object and then use get_field('field-name', $post_object->ID)
more than likely the meta values have not been cached. This is one of the reasons for using a loop as shown for a relationship field because calling setup_postdata($post);
causes WP to get all the meta values for a post in a single call and cache them. This can be simulated by doing get_post_meta($post_object->ID);
which results in the same thing, WP gets all of the meta values for the post in a single query and caches them.
As far as using if () {} elseif () {} else {}
syntax or a switch statement. For me this has nothing to do with performance even if one is more performant than the other. For me it has to do with what the code looks like. Long strings of elseif
s are extremely difficult to keep straight an make the code difficult to read and maintain. I will use an, if or an if/else, or sometimes even an if/elseif/else if I’m sure that there will only ever be 1, 2 or 3 possibilities, but beyond that and in cases where I think that the cases can grow in the future will always use a switch statement. When I open someone else’s code and see 20 elsif’s with simple conditions the first thing you will here is me saying “WTF”.
But that said about elseif’s, there are times when the if conditions are too complex to be dealt with in a switch statement. These or meant for simple conditions.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.