Support

Account

Home Forums Feature Requests i need help! this is my code

Solving

i need help! this is my code

  • Hello everyone and thank you very much for the help

    I have a code that brings me all the fields from all the posts in the following way:

    
    
    // 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_field('number');
    
    		// 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;
    
    

    Now what I’m trying to do is to connect all the “number” fields. I found that if I have 3 in post 1 and 3 in the second post, then the result will be 4 connecting all the posts according to this field. Does anyone have an idea?

  • Sorry, it is not at all clear to me what you are trying to do. Can you give more detail?

  • of course
    What I’m trying to do is
    Take the field I wrote as an example, its meta is “numbers” and do a connection operation between all the posts, this means that if one post within the field has 10 and another post has 30, then in Potter I want to display 40

  • I think that what you want at the end is:

    
    $total = array_sum($all_posts); // sum of all numbers in array
    

    you can try

    
    print_r($total);
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.