Support

Account

Forum Replies Created

  • Hi fgenois,

    It seams you have the same issue as explain here. As I told to Dada, you probabely kill the query loop by using setup_postdata(), plus, when you want to get_field in a loop which is in a loop (and again and again) it’s better to call the field with the post ID.

    Try this (same logic as the Dada issue, not functional, only to show the logical path) :

    $args = array(...);
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post() ;
    		$query_post_id= get_the_ID();
    		if( have_rows('repeater_you_wan', $query_post_id)) { 
    					while ( have_rows('repeater_you_wan', $query_post_id)) : the_row(); // get row with the query_id to avoid mistake
    						[...] // Write your html
    						$posts_from_repeater = get_sub_field('your_multiple_post_object_subfield');
    						if($posts_from_repeater) {
    							foreach ($posts_from_repeater as $sub_post) { // Where $sub_post is post object
    								$sub_post_id = $sub_post->ID;
    								$my_post_field_data = get_field('my_post_field_data', $sub_post_id);// get field with the query_id to avoid mistake
    								$my_post_title = get_the_title($sub_post); // can get title by post object
    								[...] // Write your html
    								[...] // Write your html
    								[...] // Write your html
    							}
    						}
    						[...] // Write your html
    					endwhile;
    		}
    	}
    }
    wp_reset_postdata();

    PS : when you write code, use the code balise it’s will be easier to read.

  • Hi Dada

    You probabely kill the query loop by using the setup_postdata(). Try this way (Not functional, just to show you the logical path) :

    $args = array('posts_per_page' => '-1','post_type'	=> 'listas-compras','author' => $user_ID);
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post() ;
    		$listas_id= get_the_ID();
    		$asignar_recetas = get_field('asignar_receta', $listas_id);
    		if ($asignar_recetas) {
    			[...] // Write your html 
    			foreach( $asignar_recetas as $receta) {
    				// each 'receta' is recetas post object
    				$receta_id = $receta->ID;	
    				[...] // Write your html
    				echo '<a href="'.get_permalink($receta).'>">'.get_the_title($receta).'</a>'; // get post data with the post object
    				[...] // Write your html
    				if( have_rows('ingredientes', $receta_id)) { 
    					while ( have_rows('ingredientes', $receta_id)) : the_row(); // get row with the receta_id to avoid mistake
    						[...] // Write your html
    						the_sub_field('cantidad');
    						[...] // Write your html
    						$ingrediente = get_sub_field('ingrediente'); // This is the ingrediente post object
    						[...] // Write your html
    						echo get_the_title($ingrediente);
    						[...] // Write your html
    					endwhile;
    					[...] // Write your html
    				}
    				[...] // Write your html
    			}
    			[...] // Write your html
    		}
    		[...] // Write your html
    	}
    }
    wp_reset_postdata();
  • Hi Dada,

    I think it’s not working because each time you used setup_postdata() you probably kill the query loop called above.

    Try this way (not functional, just to show you the logical path) :

    
    $args = array('posts_per_page' => '-1','post_type'	=> 'listas-compras','author' => $user_ID);
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post() ;
    		$listas_id= get_the_ID();
    		$asignar_recetas = get_field('asignar_receta', $listas_id);
    		if ($asignar_recetas) {
    			[...] // Write your html 
    			foreach( $asignar_recetas as $receta) {
    				// each 'receta' is recetas post object
    				$receta_id = $receta->ID;	
    				[...] // Write your html
    				echo '<a href="'.get_permalink($receta).'>">'.get_the_title($receta).'</a>'; // get post data with the post object
    				[...] // Write your html
    				if( have_rows('ingredientes', $receta_id)) { 
    					while ( have_rows('ingredientes', $receta_id)) : the_row(); // get row with the receta_id to avoid mistake
    						[...] // Write your html
    						the_sub_field('cantidad');
    						[...] // Write your html
    						$ingrediente = get_sub_field('ingrediente'); // This is the ingrediente post object
    						[...] // Write your html
    						echo get_the_title($ingrediente);
    						[...] // Write your html
    					endwhile;
    					[...] // Write your html
    				}
    				[...] // Write your html
    			}
    			[...] // Write your html
    		}
    		[...] // Write your html
    	}
    }
    wp_reset_postdata();
  • Hi John,

    I already use the ACF documentation, and the forum thread you listed is useful but I read your advice and I’ll change my way to do 🙂

    Yes I need the repeater field because I used it 99% for only store complexe and related datas which are not used in query but fill by several staff members (and it’s very userfriendly). For the last 1% I’ll look for a simple solution based on your advices.

    Thanks !

  • Hi,

    When you have a loop in a loop, the function like get_the_content() or get_field_object(‘autor’) dont know if they must use the ID of the first “looped” object or the second one …

    To solve it, get the id in variable and use it to clearly indicate which object is focused :

    
    if ( $wp_query->have_posts() ) : ?>
             <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
                              // Set variables
                              $id = get_the_ID()
                              $title = get_the_title($id );
                              $description = get_the_content($id ); 
                              $field = get_field_object('autor', $id );
                              $colors = $field['value'];
                              $date = get_the_date( 'd.m.Y' );
                              // Output
                              ?>
                                <?php echo $colors;  ?>
                                <?php $wp_query->reset_postdata(); ?>
              <?php endwhile; ?>
       <?php endif; ?>

    Hope it helps.

  • Hi,

    It would have been more simple to help if you posted the code you used to get the User field to populate and to disabled/readonly the sub-field once it is saved.

    To put you on the path (to be confirmed depending on your code) :

    1) Add a specific class to the sub-field in ACF back-end (like “always_disabled”) and add some javascript by using this selector to always disabled the field (see in ACF/WP documentation how to add it properly).

    2) Use the “prepare_field”/”load_value” functions to populate your sub-field value (as you probably done) and to set the field to “readonly” (required to avoid weird reaction of ACF when trying to save a JS disabled input)

    3) Save the sub-field value by using “update_value” / “save_post” or other function (because the disabled and “readonly” sub-field will not be saved automatically).

    It is not necessarily the ultimate solution, but it’s work for me. Hope it helps, send you code if you want more details =).

  • I found a solution by myself. Some input type as Chekbox type have an additional hidden input so I need to disabled all the input used by ACF.

    Exemple for Chekbox :

    add_filter('acf/prepare_field', 'edit_prepare_field');
    function edit_prepare_field( $field ) {
    	...
    	case 'checkbox':
    		$field['wrapper'] = array('width'=> $width,'class'=> 'disabled_checkbox');
    		$field['readonly'] = 1;
    		break;
    	...
    	return $field;
    }
    
    function read_only_field_javascript() {
    	...
    	$('.disabled_checkbox select').prop('disabled','disabled');
            $('.disabled_checkbox input').prop('disabled','disabled');
    	...
    }

    User field is more complex (use <span> for exemple) and can’t be disabled by the previous methode, some other like gallery can really be disabled so I change the field type to “enhanced_message” and display the value with some PhP.

  • To confirm : it works perfectly ! Thanks !

    I used prepare_field in all case, to set read only if it’s text or textarea field and to change the field’s class if I need JavaScript (and disabled field or/and actions with a class selector).

  • Hum sounds like a solution =D. And I just asking myself what to do with the add and remove button of repeater fields … Perfect !

    In my case, field is disabled when “prepare_field” because I don’t know how much field I have and which permission is associated to (it’s set in the Edit Fields screen, with the settings fields I added) but I can add specific class during “prepare_field” and add the Javascript after with class selector.

    I’ll try …

  • the “echo and get” solution is normally a “not in loop” solution. Unfortunatelly, outside a loop (so, in our case), if your not having a “created user excerpt” get_the_excerpt is empty. If you just want a “cut” content to make an excerpt you can use this :

    
    <?php 
    $content_post = get_post($query_id);
    $content = $content_post->post_content;
    $excerpt = wp_trim_words( $content , 20, '...'); 
    echo $excerpt;
    ?>
    

    Where “20” is the char’ length of the excerpt you want, and “…” the text after the excerpt if the content was cut.

  • Hi,

    Not sure about what’s wrong but I already have this issue ( When get post object inside a post query and display data). I solve it by register each post ID and get data with the ID ( I think WP is a bit confused =D) …

    Try to save the current query post ID :

    ...
    <?php while($custom_query01->have_posts()) : $custom_query01->the_post(); ?>
    <?php $query_id = get_the_ID(); // THE CURRENT POST OF QUERY ID ?>
    ... 

    Do the same for the object you get :

    ...
    if( $post_object01 ): 
    $object_id = $post_object01->ID; // THE OBJECT ID
    ...

    And display all your data with echo and “get” like that :

    ...
    <?php echo get_the_title($object_id); // USE THE ID ?>
    ...
    <a href="<?php echo get_the_permalink($query_id); ?>" class="btn btn-warning">Daten & Preise</a>
    ...
Viewing 11 posts - 26 through 36 (of 36 total)