Support

Account

Forum Replies Created

  • You can add the new text field called Cena to that Field Group’s Repeater (Pozycje przeznaczenia).

  • What is your bild subfield returning? Are you able to share the output of the code? Are you referring to the title attribute of the image?

  • You can wrap the entire inner function in an if statement to avoid running any code if the field value is not empty.

    
    if ( empty( $value ) ) :
        // contents of {yl_create_unique_id_acf_field} function.
    endif;
    
  • On first glance, it looks like $arrival = $_POST['arrival-field']; is different than what’s being sent in the data object (arrival_field: arrival_field). One has a hyphen, while the other has an underscore.

    Can you verify that the ACF data is being successfully sent along to the function? By printing or console logging?

  • Hey there,

    Sorry for the late reply, been swamped the past few days. Glad you figured it out!

    Best,
    Josh

  • In that case, you’ll want to use the WP Post Object. You can then add the ACF fields array to each post. I’ve tried to make the comments as verbose as possible. I hope this helps!

    
    <?php 
    
    // Get arguments for all posts
    $args = array( 
    	'posts_per_page' => -1,
    );
    
    // Create the array for all posts AND all ACF fields
    $all_posts = array();
    $all_acf_fields = array();
    
    // Set up the query
    $the_query = new WP_Query( $args );
    
    // Setup the loop
    if ( $the_query->have_posts() ):
    
    	while ( $the_query->have_posts() ): $the_query->the_post();
    
    		// Get WP Post Object
    		$fields = get_post();
    
    		// Get ACF fields
    		$acf_fields = get_fields();
    
    		// Push each $fields object into the $all_posts array
    		array_push($all_posts, $fields);
    
    		// Push each ACF array into the $all_acf_fields array
    		array_push($all_acf_fields, $acf_fields);
    
    	endwhile;
    
    	// Restore original Post Data
    	wp_reset_postdata();
    
    	// Arbitrary array counter
    	$i = 0;
    
    	/* 
    	 * The magic
    	 * For every post in the array of $all_posts,
    	 * add a new object key of "acf_fields" to the post object.
    	 * 
    	 * This contains the array of ACF fields for that post.
    	 *
    	 * Lastly, increment the array for each post.
    	 */
    				
    	foreach ($all_posts as $p):
    		$p->acf_fields = $all_acf_fields[$i];
    		array_push($all_posts, $p);
    		$i++;
    	endforeach;
    
    	// Print the result here and do what you choose
    	echo '<pre>';
    	print_r($all_posts);
    	echo '</pre>';
    
    endif;
    
    ?>
    
  • The following code will get all posts, get an array of all the fields for the current post in the loop, and push those arrays into a new array called $all_posts. Hope this helps!

    
    // Get arguments for all posts
    $args = array( 
    	'posts_per_page' => -1,
    );
    
    // Create the array for all posts
    $all_posts = array();
    
    // Set up the query
    $the_query = new WP_Query( $args );
    
    // Setup the loop
    if ( $the_query->have_posts() ):
    
    	while ( $the_query->have_posts() ): $the_query->the_post();
    
    		// Get all fields
    		$fields = get_fields();
    
    		// Push each $fields array into the $all_posts array
    		array_push($all_posts, $fields);
    
    	endwhile;
    
    	// Restore original Post Data
    	wp_reset_postdata();
    
    	// Print the result here and do what you choose
    	print_r($all_posts);
    
    endif;
    
  • What version of ACF are you using? It’s probably a jQuery conflict somewhere else in your install. Have you tried disabling all other plugins and/or using a default theme?

  • I’m not sure the field type, but you can populate a select field like this. Place the following in your functions.php file.

    
    function acf_load_field_choices( $field ) {
    
      	// Create an empty array
    	$field['choices'] = array();
    
    	// Populate field with the current post id
    	$field['choices'][0] = get_the_ID();
    
      	// Return the result
    	return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');
    
  • No problem! In that case, try this.

    
    function acf_load_field_choices( $field ) {
    
      	// Create an empty array
    	$field['choices'] = array();
    
      	// Get parent categories and order by name
    	$categories = get_categories( 
    		array(
    			'orderby' => 'name',
    			'hide_empty' => 0,
    		) 
    	);
    
      	// Create a counter variable for the array
    	$i = 0;
    
    	foreach( $categories as $category ) {
    
    		// Get the top level categories
    		if ( $category->parent == 0 ) {
    
    			$field['choices'][$i] = $category->name;
    
    			$i++;
    
    			// Get subcategories within the parent category only
    			foreach ( $categories as $subcategory ) {
    
    				if ($subcategory->parent == $category->term_id) {
    
    					$field['choices'][$i] = $subcategory->name;
    					$i++;
    				}
    			}
    
    			$i++;
    
    		}
    	}
    
      	// Return the result
    	return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');
    
  • Assuming I’ve understood your post correctly, replace YOUR_FIELD_NAME_HERE with the field name of your relationship field.

    
    <?php 
    
    	// Get all school_class posts
    	$args = array( 
    		'post_type' => 'school_class', 
    		'posts_per_page' => -1,
    	);		
    
    	// Generate the loop
    	$loop = new WP_Query( $args );
    
    	while ( $loop->have_posts() ) : $loop->the_post();
    
    		// Get the relationship field_name that you've defined
    		$posts = get_field('YOUR_FIELD_NAME_HERE');
    
    		if ( $posts ):
    
    			foreach ( $posts as $post):
    
    				// Set up the post for posts with that field_name
    				setup_postdata($post); 
    
    				// Narrow the posts by the "easy" category
    				if (has_category('easy')):
    
    					// Print the whole post object
    					// Do what you want from here
    					print_r($post);
    
    				endif;
    
    			endforeach;
    
    			// Reset the postdata so the page works correctly
    			wp_reset_postdata();
    
    		endif;
    
    	endwhile;	
    
    ?>
    
  • Assuming I’ve understood your request correctly, replace YOUR_FIELD_NAME_HERE with the field name of the relationship field you’ve created under the school_class custom post type.

    
    <?php 
    
    // Get all school_class posts
    $args = array( 
    	'post_type' => 'school_class', 
    	'posts_per_page' => -1,
    );		
    
    // Generate the loop
    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post();
    
    	// Get the relationship field_name that you've defined
    	$posts = get_field('YOUR_FIELD_NAME_HERE');
    
    	if ( $posts ):
    
    		foreach ( $posts as $post):
    
    			// Set up the post for posts with that field_name
    			setup_postdata($post); 
    
    			// Narrow the posts by the "easy" category
                // Refer to https://codex.wordpress.org/Function_Reference/has_category for more options
    			if (has_category('easy')):
    
    			        // Print the whole post object
    			        // Do what you want from here
    			        print_r($post);
    
    		     endif;
    
    		endforeach;
    
    		// Reset the postdata so the page works correctly
    		wp_reset_postdata();
    				
            endif;
    
    endwhile;	
    
    ?>		
    
  • We’ll need more information to answer your question entirely, but hopefully this code can get you started.

    You mention that you need to narrow it down to one parent category, but then display a list of categories, so I’m not sure I’m understanding fully.

    The following example gets all top level categories from the default WordPress post type and populates an ACF select field called “test”. The following code should be inserted into your functions.php file.

    
    function acf_load_field_choices( $field ) {
    
      // Create an empty array
      $field['choices'] = array();
    
      // Get parent categories and order by name
      $categories = get_categories( 
        array(
          'orderby' => 'name',
          'parent'  => 0
        ) 
      );
    
      // Create a counter variable for the array
      $i = 0;
    
      foreach ( $categories as $category ) {
    
        // Add each category to the array
        $field['choices'][$i] = $category->name;
    
        // Increment the loop counter
        $i++;
    
      }
      
      // Return the result
      return $field;
    
    }
    
    add_filter('acf/load_field/name=test', 'acf_load_field_choices');
    
Viewing 13 posts - 1 through 13 (of 13 total)