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 an author page, and I want to list posts from current author's categories.

e.g. I'm viewing John's page, and John wrote in Sport, Tech, News, Computer categories.

Then my code list from this (Sport, Tech, News, Computer) categories list random 10 post.

But something is going wrong:

Those 4 categories have 25 posts. But my code isn't listing 10 posts, it's just listing 1 post. And i need random post list

My code:

<?php global $post, $wpdb;
$author_id = $post->post_author;
$categories = $wpdb->get_results("
    SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug
    FROM $wpdb->posts as posts
    LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
    LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
    WHERE 1=1 AND (
        posts.post_status = 'publish' AND
        posts.post_author = '$author_id' AND
        tax.taxonomy = 'category' )
    ORDER BY terms.name ASC
");

foreach($categories as $category) : 
    $catnumber = $category->ID.',';
endforeach;?>
<?php 

$args = array(
'category__and' => array( $catnumber )
, 'showposts'=> '10'
);

$my_query = new WP_Query( $args );
?>
<ul>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>  
    </li>
<?php endwhile; ?>
<ul>
<?php wp_reset_postdata(); ?>
share|improve this question
    
What's not working in this code? –  brasofilo Jan 26 '13 at 18:04
    
im viewin an author page, author written 4 category, 4 category have 25 post.. but my code not listing 10 posts just listing 1 post. And i need random post list. –  Genxer Jan 26 '13 at 18:08

1 Answer 1

up vote 1 down vote accepted

Granted, I may be misinterpreting you. Your post is not very clear but ...

category__and will require the posts to be in both categories. It sounds like you need posts in any of the categories. You should be using category__in so that you get an OR match on the categories instead of an AND match.

showposts is deprecated.

Both category__and and category__in accept an array. You are building a comma separated string of categories and then casting it as an array. That is not likely to work as expected.

Build your category array like this:

foreach($categories as $category) : 
    $catnumber[] = $category->ID;
endforeach;

And query like this:

$args = array(
    'category__in'    => $catnumber,
    'posts_per_page'  => 10,
    'orderby'         => 'rand'
);

http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

share|improve this answer
    
I changed categor__in but still same.. Im viewin an author page, my code not listing 10 posts just listing 1 post. And i need random post list –  Genxer Jan 26 '13 at 18:24
    
That is exactly what you said in another comment. That doesn't help. –  s_ha_dum Jan 26 '13 at 18:25
    
I used 'category__in' but not changed, still listing just 1 post. maybe you didnt understand me for my english.. Can you check this picture maybe you can understand me good. Regards –  Genxer Jan 26 '13 at 18:40
    
It's worked.I saw your new code.Regards –  Genxer Jan 26 '13 at 18:45

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.