Support

Account

Home Forums Backend Issues (wp-admin) Short by field in array

Unread

Short by field in array

  • Hello,
    I have made a custom form where I want to short and fetch posts depending on answers score on form submit. I have an array and I want on submit to show the first 3 posts titles and permalink shorting by the highest score. I have made the Code and since Im newbie any help much appreciated. :

    <?php	
    				
    $change_result=0;				
    /// all posts
    // Get arguments for all posts
    $args = array( 
    	'posts_per_page' => -1,
    	'post_type'			=> 'post',
    	'meta_key'			=> 'total_score',
    	'orderby'			=> 'meta_value',
    	'order'				=> 'DESC'
    	
    );
    
    // Create the array for all posts
    $all_posts = array();
    
    	// Set up the query
    $the_query = new WP_Query( $args );
    
    while ( $the_query->have_posts() ): $the_query->the_post();
    
    				
    				
    		// Get all fields
    		$fields = get_fields();
    				
    			
    				
    ?>
     <li>
    			<a href="<?php the_permalink(); ?>"> 
    				
    				<?php the_title(); ?>
    			</a>
    		</li>				
    <?php				
    				
    		// Push each $fields array into the $all_posts array
    		array_push($all_posts, $fields);
    
    	endwhile;
    
    	// Restore original Post Data
    	wp_reset_postdata();
    
    //print_r($all_posts);		
    
    		
    
    				
     //question1				
    				
    //CORECT CODE !!!!				
    ///	
     $score=0;
    
    if(isset($_POST['place'])) 
    { // start the loop
    	
    $q1=$_POST["place"];
    	
    //CORECT CODE !!!!	
    foreach($all_posts as &$value) {
    if ($value['question1']==$q1){
    
    $value['total_score']=$q1;	
     
    }
    	
    }	
    //echo "Q1"."<br>";				
    //print_r($all_posts);	
    	
    
    	
    } //end question 1
    
    				
    				
    				
    // question 2
    if(isset($_POST['home']))
    { // start the loop
    $q2=$_POST["home"];	
    
    	
    //CORECT CODE !!!!	
    
    foreach($all_posts as &$value) {
    if ($value['question2']==$q2){
    
    $value['total_score']=$value['total_score']+$q2;	
    }
    //echo $value['total_score']."<br>";	
    }	
    //echo "Q2"."<br>";				
    //print_r($all_posts);
    	
    
    } //end question 2				
    
    				
    				
    // question 3
    if(isset($_POST['hours']))
    { // start the loop
    $q3=$_POST["hours"];	
    
    	
    //CORECT CODE !!!!	
    
    foreach($all_posts as &$value) {
    	
    
    if ($value['question2']==$q3){
    
    $value['total_score']=$value['total_score']+$q3;	
    }
    echo $value['total_score']."<br>";	
    }	
    //echo "Q2"."<br>";				
    
    print_r($all_posts);
    
    while ( $the_query->have_posts() ): $the_query->the_post();
    
    	$class = get_field('total_score') ? 'class="total_score"' : '';			
    				
    		// Get all fields
    		$fields = get_fields();
    				
    			
    				
    ?>
     <li>
    			<a href="<?php the_permalink(); ?>"> <?php the_field('total_score'); ?>
    				
    				<?php the_title(); ?>
    			</a>
    		</li>				
    <?php				
    				
    		// Push each $fields array into the $all_posts array
    		//array_push($all_posts, $fields);
    
    	endwhile;
    	
    
    	
    	
    
    } //end question 3	
    				
    				
    			
    				
    				
    ?>				
    <!-- custom code end -->	
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.