skip to Main Content

Im new to WP Query and ACF custom fields. I want to write a code that will show first 3 results from the calculation of the total_score custom field. I have managed to short by the total score but I want to show the title and permanlink of the first 3 posts so the visitor will click and go to the post. Any help will be much appreciated. My code so far :

$args = array( 
'posts_per_page' => -1,
'post_title'    => true,);
$all_posts = array();
$the_query = new WP_Query( $args );
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;
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;  
}
}   } //end question 1
// question 2
if(isset($_POST['home']))
{ // start the loop
$q2=$_POST["home"];
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 "Q2"."<br>";             
} //end question 3  
            
// shorting by total_score
function sortByOrder($a, $b) {
return $b['total_score'] - $a['total_score'];
}
usort($all_posts, 'sortByOrder');               
   //print_r($all_posts);   
foreach($all_posts as &$value) {
echo $value['total_score']."<br>";  
}               

            




    

2

Answers


  1. You could look at using your $args variable to both limit and filter the results of WP_Query. Setting your 'posts_per_page' => 3. This means only 3 posts will be returned in the query.

    However, as you are fetching all of the posts and using usort to sort them, you can swap this out to use this resource to help you query using a custom field. This will reduce the amount of work is needed to compile the posts you require.

    You can then use the final result to loop over the 3 posts and output your title & permalink.

    Login or Signup to reply.
  2. Please replace your code with only this code. It’s give you only 3 post and your total_score ASC or DESC

    and Replace data in as per comment

    Let me know if any Query

    <?php
    $args = array(
        'post_type' => 'portfolio', // your post type name
        'orderby' => 'meta_value_num', // if total_score is string then use it - meta_value
        'meta_key' => 'total_score', // your meta key name ( total_score custom field )
        'posts_per_page' => 3,
        'order' => 'ASC' // ASC or DESC
    
    );
    $loop = new WP_Query( $args );
    
    if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();
        echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a>';
    endwhile;
    endif;
    
    wp_reset_postdata();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search