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

  • Hi John!

    Reading post object, in the end it will be around 20+ different post objects and 5-10 different categories. Each category will have 5-20 post objects.

    See below if this make sense.

    $post_object = get_field('post_object_categoryONE_1','options')
    $post_object_2 = get_field('post_object_categoryTWO_1','options')

    And this is the code part we are referring to:

    $post_object = get_field('post_object_categoryone_1','options'); // Set field to grab
    if( !empty ($post_object) );
            $imageArray_BG = get_field('post_hero_img_1', $post_object->ID); // Array returned by Advanced Custom Fields
    		$imageAlt_BG = esc_attr($imageArray_BG['alt']); // Grab, from the array, the 'alt'
    		$imageURL_BG = esc_url($imageArray_BG['url']); // Grab the full size version

    One post object contain one post. I see now what you mean, relationship would be smarter code and query once to receive all meta. I didn’t think about each new DB queries here. But in this case I need to fetch one post per post object strictly even if it from the side doesn’t make sense.

    Regarding the conditions it displays a different post object category based on which category the page have selected which is a select field on the page. Like if we would have 20 post objects fruits and 20 post object vegetables. Then the first post object in fruits would show apple while if the category was vegetables it would show carrot as the first post object field.

    But with what you said I should maybe skip conditions here and make 10 different blocks as this would reduce the DB queries, as each block would then represent the conditions. That way I would limit the calls to one extent. Which maybe wouldn’t have been an issue with the relationship field as you explained using one call to fetch all posts in one query.

    But looking at a whole page with other plugins DB calls such as WPML and other together with other page custom fields the amount of fields maybe is much it’s relative I guess, the number must be compared to each function what you try to do to determine if it’s much or not. But logged in I have just as an example SQL (233 queries|62.61 ms). It might still be ways to optimize the DB queries here even if I’m not going with the relationship fields, as one example as you suggested below regarding get_post_meta()

    Is there an easy way to check if the queries is cached or not? I also use redis object cache. But I anyhow wouldn’t prefer to rely on cache it should be beneficial not a must.

    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.

    Could you show me this in one of my examples above, I didn’t make it work. Where would you like to place it?

    Thanks for the answer regarding if/elseif/else and switch conditions.