Support

Account

Forum Replies Created

  • Thanks as always John!

    Found out my default code is working I was just loading another block which did not have an solution if empty/switch for number_fromat(); which was causing the issue.

    Below works and the same using if only, but I stick with switch easier to structure if you don’t see something I’m missing or wrong doing?

    <?php
    	$value = get_field('data_value');
    
    				switch ($value){
    					case null:
    						echo '-';
    						break;
    					default:
    						echo esc_attr($pre_value) . number_format($value);
    				}
    ?>
    <?php 
    	$value = get_field('data_value');
    
    					if ($value):
    						echo esc_attr($pre_value) . number_format($value);
    					else: echo '-';
    				endif;
    ?>
  • If I would for some reasons convert to relationship can I still use get_field without post_object->ID? As I prefer get_field queries to structure the quries before echo them over the_field directly into the echo.

    Just having a quick look at https://www.advancedcustomfields.com/resources/relationship/ where the_field is used once using setup_postdata();. Just did a quick test but even if I setup setup_postdata(); I need to call

    within the foreach loop:

    		$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 

    Which means all posts in the relationship is not passed in one query, correct?

  • I honestly don’t know if there is a way to tell if meta values have already been cached. When I’m trying to figure this out I generally install a query monitor plugin and test first with and then without the call to get_post_meta().

    Using https://wordpress.org/plugins/query-monitor/ does any of them actually say based on real queries cached or not? Using both but right now using https://wordpress.org/plugins/blackbar/

    If the queries are reduced I leave it in.

    What do you mean with leave it in? If queries is reduced it means not cache, or am I misunderstanding you or just getting late here..

    Another thing you might consider is if you’re actually going to use the post data. Will you use the post title, excerpt, content, ect, OR will you only be using custom fields from those posts. If you will not be using standard post field content then you don’t really need the post object and just the ID would be needed to get the custom fields. You can do this by either setting the field to return an array or by get_field('post_object', false, false);

    In this I only use custom fields. Could you give me a full visual example as I don’t think I understand what you mean with “or by get_field('post_object', false, false);

  • 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.

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