Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have many post, all categorized. So 100 post, 40 in cat=4. All those 40 post have a custom field name point with a rating in there (how many point win). Si the question How to loop through all the post, and get ONLY the post with cat=4, and sort those post base on the point score in the custom field, and display, post title, and point. So the output will look like :

  • john : 134pt
  • paul : 124pt
  • kim : 100pt
  • joe : 90 pt
share|improve this question

2 Answers 2

up vote 1 down vote accepted

See WP_Query in Codex for info on how to query on custom fields.

$args = array(
    'posts_per_page' => -1,
    'cat' => 4,
    'meta_key' => 'point_score',
    'orderby' => 'meta_value_num'
);

$points_query = new WP_Query( $args );

while( $points_query->have_posts() ):
    $points_query->the_post();
    the_title();
    echo ' : ';
    echo get_post_meta( $post->ID, 'point_score', true );
endwhile;
share|improve this answer
    
get_post_meta echo the output.. i like to have it in a var... exept for that... the rest look like working –  menardmam Jun 11 '12 at 18:47
    
that page answer a lot of questions : codex.wordpress.org/Custom_Fields –  menardmam Jun 11 '12 at 20:01

For future reference, here is the final working code

function mam_listing($args) {

    $args = array(
    'posts_per_page' => -1,
    'cat' => 6,
    'meta_key' => 'Points',
    'orderby' => 'meta_value_num'
    );

$outputt = '<table width="100%" cellpadding="2" border="0">';
$no=0;

$points_query = new WP_Query( $args );

while(  $points_query->have_posts() ):
        $points_query->the_post();

        $no = $no+1;

        $outputt .= '<tr>';

        $outputt .= '<td>';
        $outputt .= 'Position '.$no;
        $outputt .= '</td>';

        $outputt .= '<td>';
        $outputt .= '<a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) .'</a>';
        $outputt .= '</td>';

        $outputt .= '<td>';
        $temp = get_post_custom_values("Points");
        $outputt .= $temp[0];
        $outputt .= " Points";
        $outputt .= '</td></tr>';
endwhile;

$outputt .= '</table>';

    return  $outputt;
}

add_shortcode( 'mam-listing', 'mam_listing' ); 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.