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.

On my homepage, I have 3 columns showing varying amounts of information from posts in specific categories (first 2 of them child categories, the third column displays posts from various child categories, but one parent category).

I want to determine which posts to display (by category as described) in each, by making them sticky.

Help?

share|improve this question
2  
What have you tried so far? –  dalbaeb Apr 24 '13 at 21:04
    
I tried using 3 loops with some measure of success (my PHP skills are crap, so I'll refrain from posting that here to avoid embarrassment), but I'm hitting 2 walls ... 1. I can get a maximum of 5 sticky posts to display across all columns 2. The method I used gets the category ID from category slug and can therefore not be used on parent category level to catch all posts in child categories of that parent. –  Jayx Apr 24 '13 at 21:08
    
Still, would be great to see that code. For 5 posts issue, can try supplying the posts_per_page pagination parameter. –  dalbaeb Apr 24 '13 at 21:11
2  
Your question will fair far better if you post your crappy PHP than if you don't. –  s_ha_dum Apr 24 '13 at 21:19
add comment

2 Answers

OK ... code might still be a bit on the flimsy side, but here's what I came up with (still not posting the old, even crappier, PHP; sorry down-voter ) in case anyone else might have use for it or can improve on it.

<section class="column first">
  <h2>What's New</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'news',
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column middle">
  <h2>Country Projects</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'projects',
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column last">
  <h2>Featured Publications</h2>
  <?php 
    $sticky = get_option( 'sticky_posts' );
    $args = array(
      'category_name' => 'publications', //This is the parent category, no need for using the cat ID this way.
      'post__in'  => $sticky,
    );
    query_posts( $args ); while (have_posts()) : the_post();
  { ?>
  <h3><?php the_title(); ?></h3>
  <?php the_content(); ?>
  <?php } endwhile; wp_reset_query(); ?>
</section>

Feel free to improve this or comment on how it can be optimised and/or pros and cons to my approach.

share|improve this answer
1  
It would be better to avoid use of query_posts - see here. Also, you could optimize the performance of your code by only using one wp_query (as opposed to query_posts), then iterating through the results of that one query to build and organize a new array for your output. That would reduce your DB hits from 5 to 1. –  vancoder Apr 24 '13 at 23:42
    
Thanks for your comment and for the tip @vancoder - I don't want to spam by posting the final solution, but suffice it to say that each of those blocks have very different markup and functionality. Could you possibly post an example of what you suggest as an answer? –  Jayx Apr 25 '13 at 19:29
add comment

As requested, this is a rough example:

<?php
$args = array(
    'posts_per_page' => -1,
    'category__in' => array( 1, 2, 3 ), // IDs of your categories
    'post__in' => get_option( 'sticky_posts' ),
);
$query = new WP_Query( $args ); // do one query only

while ( $query->have_posts() ) {
    $query->the_post();
    $categories = get_the_category();
    $categorized_posts[$categories[0]->name][] = array( get_the_title(), get_the_content() ); // build a new array
}
?>

<section class="column first">
    <h2>What's New</h2>
    <?php
    if ( isset( $categorized_posts['news'] ) ) {
        foreach ( $categorized_posts['news'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>

<section class="column middle">
    <h2>Country Projects</h2>
    <?php
    if ( isset( $categorized_posts['projects'] ) ) {
        foreach ( $categorized_posts['projects'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>

<section class="column last">
    <h2>Featured Publications</h2>
    <?php
    if ( isset( $categorized_posts['publications'] ) ) {
        foreach ( $categorized_posts['publications'] as $post_stuff ) {
            echo '<h3>' . $post_stuff[0] . '</h3>';
            echo $post_stuff[1];
        }
    }
    ?>
</section>
share|improve this answer
add comment

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.