Question

I'm sure this question is asked alot, but I cannot figure it out, since all the different ways to query posts are confusing me..

Here is my current code.
How can I tell the code to only get posts from category ID #22?

<?php 
$querystr = " 
    SELECT post_id, AVG(meta_value) AS total, COUNT(post_id) as num_karakter
    FROM sk_postmeta
    WHERE meta_key = 'krit_karakter'
    GROUP BY post_id
    ORDER BY total DESC
    LIMIT 5
    "; 
$pageposts = $wpdb->get_results($querystr, OBJECT); 

if ($pageposts) { ?>
    <ul class="smartbar-items">
    <?php foreach ($pageposts as $post): 
        setup_postdata($post); ?> 
        <li><a href="<?php echo get_permalink($post->post_id);?>"><img src="<?php bloginfo('url');?>/wp-content/files_mf/<?php echo get_post_meta($post->post_id, 'game_poster', true); ?>" width="80" height="110" alt="<?php echo get_the_title($post->post_id);?>" />  <br /><?php echo get_the_title($post->post_id);?></a></li>
    <?php  endforeach; ?>
    </ul>

Thanks for all help!

Was it helpful?

Solution

I would do this in two pieces.

  1. Add this into your functions.php file to grab custom fields globally:

    function get_custom_field($key, $echo = FALSE) {
    global $post;
    $custom_field = get_post_meta($post->ID, $key, true);
    if ($echo == FALSE) return $custom_field;
    echo $custom_field;
    

    }

  2. Build your query as follows:

I can't seem to get markdown to play nice, so here's the code in Pastie http://pastie.org/1294594

OTHER TIPS

You would need to join in the sk_term_relationships table.

$querystr = " 
 SELECT post_id, AVG(meta_value) AS total, COUNT(post_id) as num_karakter
 FROM sk_postmeta p
 inner join sk_term_relationship t on p.post_id = t.object_id 
  and t.term_taxonomy_id = 22
 WHERE meta_key = 'krit_karakter'
 GROUP BY post_id
 ORDER BY total DESC
 LIMIT 5
"; 
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top