1

Please bare with me as I am self taught and trying to learn as much as possible. I am going to try and provide as much information as possible that I have learned from researching this problem.

My Goal/The Problem: Allowing readers to sort a category of posts using either a clickable link or drop down form. For now, we will just stick with clickable links "Sort by Date" and "Sort by Title". How do I accomplish this? What is the best method to do so?

What I have- The Base: Thanks to users at Stack Exchange, I have been able to accomplish retrieving a page of posts in a category that matches the name of the page. If the page is named Show Rants, then it'll spit out all the posts in the category Show Rants. Here is what this looks like (thanks Peter G).

This is related because I would like to keep this same format of retrieving posts by page name=cat name.

<?php
$args = [
'category_name' => get_queried_object()->post_name,
'order'         => 'DESC',
'orderby'       => 'date(post_date)' 
// Add any extra parameters you need
];
$q = new WP_Query( $args ); 


// Run the loop
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
get_template_part( 'content', get_post_format() );
}
wp_reset_postdata();
}
?>

In the above example we are using WP_Query, which is important for me to note (mostly for myself) cause I was using query_posts up until then.

So is the result of one sort type on the backend. Now I want to do a multiple orderby on the front end.

Here are the methods I've encountered or attempted:

query_post: I was previously working on a query_posts sort that worked. The first problem with this is that it is query_posts! This week I learned that it is definitely not the route I want to go. WP_Query is far more superior and safer. Here is the gross code I have anyways. The second problem is that it does not summon posts from the category that the page was named after (which is way less of a problem)

<?php $sort= $_GET['sort'];
if($sort == "title"){
$order= "orderby=title";
}
if($sort == "date"){
$order= "orderby=date";
}
?>

<?php get_header(); ?>

<div class="content-container">

<h2><?php the_title(); ?></h2>

<a href="?sort=title">Sort By Title</a>
<a href="?sort=date">Sort By Date</a>

<?php query_posts($order.'&order=ASC');  ?>

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

<li>    <a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>

    <?php endwhile; 
    endif; 
    wp_reset_query();?>
</ul>

</div>

Now if I could get this to be in WP_Query, then we can be all set. Except I'm still rather limited to figuring that out. Which is what my next attempt was going to be.

WP_Query: Frankensteining the two above examples along with what I've learned in the Codex, I created code that not surprisingly does not work. Even though I know it's absolutely wrong, I will post it anyways, so spare the ridicule. I would like to know if I was close AT ALL.

<?php
$args= [
'category_name' => 'animoo-reviews',
'order'         => 'DESC',
'orderby'       => 'date(post_date)'
];
$A = new WP_Query( $args );

while ( $A->have_posts() ) {
$A->the_post();
echo '<li>' .get_the_title() .'</li>';
}

wp_reset_postdata(); /*instead of wp_reset_query?*/

/* 2nd Query Sort? */
$args2= [
'category_name' => 'animoo-reviews',
'order'         => 'ASC',
'orderby'       => 'title'
 ];

$B = new WP_Query($args2);

// Loop 2?
while( $B->have_posts()) {
$B->the_post();
echo '<li>' .get_the_title() .'</li>';
}

//Restoring original post data
wp_reset_postdata();
?>

The problem is it works, but not how I thought it would and I don't even know how to link it so the user can sort. As I should have guessed, it's going to pop out the first 6 posts in one sort order and then pop out another six posts in the other order. Closer, but not really all that close.

Edit: Woops, I realized this was just basically the Codex's Multiple Loop example. I tried to connect it to urls but that didn't work. I'll just leave it here cause I want to show that I at least attempted something in relation to the problem. For category_name, I can always use get_queried_object->post_name. I was just simplifying it while I was playing with it before.

add_query_arg: Going on days of research, I have stumbled across this option a few times. In theory, from how people talk about it, it seems easy. Give a URL that will sort posts one way and another URL that will sort posts another way. I have to manipulate the functions.php file for it as well. I am planning on tackling this today after I've looked into another problem for my project.

It would be endlessly appreciated if someone can show me the way to go about this. I am rather new to this but indeed I am learning leaps and bounds every day. Sometimes things just don't seem as obvious to me as it seems to others (like things written in the Codex).

Once again, the goal:

Allowing readers to sort through posts in more than one way. (Optional but would be coolbeans) If the posts can come from the category the page is named after.

What I don't want:

A plugin. You may go ahead and suggest a handy plugin of you wish, but I really need to learn this from the ground up. Grabbing a plugin for everything is only going to cripple my learning in the beginning.

Thank you, once again, for your time.

  • +1 and with your dedication I think you're nearly there. You might also check out the difference between main queries vs secondary queries and how one can use the pre_get_posts filter to modify these queries. – birgire Nov 18 '15 at 16:53
4

orderby defaults to date, so we can always leave that out if we want to order by date. so the only thing we need to detect is if $_GET['sort'] is set and it's equal to title.

here we set the query args for all queries, and just add on the orderby if it's title:

$args = array(
    'category_name' => get_queried_object()->post_name,
    'order'         => 'DESC'
);

if( isset( $_GET['sort'] ) && "title" == $_GET['sort'] ){
    $args['orderby'] = 'title';
}

$q = new WP_Query( $args );
  • Thanks Milo. I've been going over your code since you posted it trying to make urls that will be clickable with it. Every time I do so, the page is breaking. I've tried several versions of the code. Been kinda hesitating to ask cause I think it may seem too obvious to others, but how would you make this work? – Nimara Nov 18 '15 at 17:20
  • 1
    If you enable debugging you'll see error messages that can help determine what's wrong. Add the code you're currently working with to your question. You should see the above query working if you append ?sort=title in the URL bar on the page you're currently viewing it, make sure that part at least works. – Milo Nov 18 '15 at 17:34
  • Alrighty. I got this: Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in /var/.../cat4-page.php on line 21. Which would be $args( 'orderby' => 'title' );. Seems pretty weird. – Nimara Nov 18 '15 at 17:48
  • oh, oops. sorry, hadn't had my coffee yet! see edit above. – Milo Nov 18 '15 at 17:52
  • No worries! I learned from the mistake as well! It works beautifully now, links and all. Thanks. – Nimara Nov 18 '15 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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