4

Hey everyone on Stackexchange!

I'm trying to find a way to prioritize posts based on a category in a loop, which is sorted by dates. All posts are grouped under their respective dates. The date labels appear once on top of posts of the same date like this:

Date

  • Post1
  • Post2
  • Post3

Date before that

  • Post 4
  • Post 5

etc

I'm trying to achieve a result, where if a post belongs to a certain predetermined category (I would only be using 1 category), it would be moved above others under a date as such:

Date

  • Post3 (special category)
  • Post1
  • Post2

Date before that

  • etc

I've done research for a while on this topic and I can't figure out whether this would even be possible or not. Some query functions seem to serve a similar purpose, but I'm not certain and the date sorting makes it harder to figure out. Would I need multiple loops or something of that nature?

My loop: http://pastebin.com/Pp0rA5j7

The loop and the sorting mechanism look like this in general (pastebin for a full code):

<?php $w_h = $w_d = $last = 0; ?>

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <?php
    if ( date('Yz') == get_the_time('Yz') ) {
        if (!$w_d++) echo '<h6>Today</h6>';
    } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
        if (!$w_h++) echo '<h6>Yesterday</h6>';
    } else {
        echo the_date('', '<h3>', '</h3>');
    }; ?>

// post content

    <?php endwhile; ?>
    <?php endif; ?>
                     <?php else : ?>

Sample site: http://goldenred.web44.net (I would like to move the post test 6 with the category "test" above others under February 3, 2013)

I would be extremely thankful if anyone more experienced could help or at least point me towards a general direction. All comments are welcome.

2 Answers 2

1

May be it's a bad idea, but it's the only.

Don't print posts immediately, but collect them in different variables: one for category "test", one for the rest.

<?php
$w_h = $w_d = $last = 0;
// init variables to concatenate content later
$primary_posts = $secondary_posts = '';
// empty array to fill later
$category_names = array();
if (have_posts()) :
    while (have_posts()) :
        the_post();
        if ( date('Yz') == get_the_time('Yz') ) {
            if (!$w_d++) echo '<h6>Today</h6>';
        } elseif ( date('Yz')-1 == get_the_time('Yz') ) {
            if (!$w_h++) echo '<h6>Yesterday</h6>';
        } else {
            echo the_date('', '<h3>', '</h3>');
        };
        // get post categories
        $category_objects = get_the_category();
        foreach($category_objects as $category_object) {
            $category_names[] = $category_object->name;
        }
        // if posts belongs to category 'test'
        if( in_array('test', $category_names) ) {
            $primary_posts .= '<div class="post">Post of category "test"';
            // title, categories and excerpt goes here
            $primary_posts .= '</div><!-- .post -->';
        }
        else {
            $secondary_posts .= '<div class="post">Post of category other than "test"';
             // title, categories and excerpt goes here
            $secondary_posts .= '</div><!-- .post -->';
       }

    endwhile;
    // output all posts of category "test"
    echo $primary_posts;
    // output all posts of category other than "test"
    echo $secondary_posts;
endif;
3
  • Thanks a ton for your contribution. I tried implementing the code, but ran into a couple of problems. I got it to display error-free, but the posts don't show up (example is in the link in my original post). I'm sure I overlooked something. My current bugged implementation looks like this: pastebin.com/7jF6Cauw I think the main problems might be with the spot where I inserted my posts into the code (currently only the title) and I think I might have to insert the category "test" instead of "name" at one point, but I'm not sure. I would be glad if you could help.
    – user27104
    Commented Feb 7, 2013 at 15:56
  • Today I see it was an embarrassing idea. My suggestion now is to go with custom meta fields for high priority posts ('test' directory) and WP_Query.
    – Max Yudin
    Commented Feb 7, 2013 at 20:51
  • Ugh. I guess this is really difficult as I expected. I will take note of your suggestions. Thanks for contributing anyway. Hopefully someone more knowledgeable can chime in on this. All suggestions or comments are welcome.
    – user27104
    Commented Feb 8, 2013 at 6:54
1

Alright I looked up WP_Query options and I started figuring if it would be easier to abolish the current date sorting system for a more efficient one that would be more flexible in terms of customization? Personally, I'm all for out of the box solutions and I'm not attached to the code I have in my index. The main goal would be to only achieve the described functionality - posts grouped by dates, and prioritizing (2 levels) within groups. Everything else could change by me. This feature isn't for a client or anything, it's for my personal website. I really need that functionality. Anyway, enough rambling, here's the code I found:

$args = array('posts_per_page' => -1, 'orderby' => 'date' );

$myQuery = new WP_Query($args);

$date = '';

if ( $myQuery->have_posts() ) : while ( $myQuery->have_posts() ) : $myQuery->the_post();

if ( $date != get_the_date() ) {
    echo $date;
    echo '<hr />';
    $date = get_the_date();
}

the_title(); // or whatever you want here.
echo '<br />';

endwhile; endif;
wp_reset_postdata();

Would something like this be a better base for what I described?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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