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.

Googled this for hours, cannot find a solution.

On my theme you have a Browse page which pulls all the videos from the Custom Post Type I am working with. If a user makes 30 categories then id like for users to be able to sort the videos using the Category buttons listed at the top. If they click on Horror id like to loop to show all Horror Videos.

How can I sort my videos using Categories within this loop?

BROWSE PAGE

My Category ul li a

<ul class="sort-by-category">
    <?
      $args = array(
            'orderby' => 'name',
            'parent' => 0
      );
      $categories = get_categories( $args );
      foreach ( $categories as $category ) {
        echo '<li><a class="browse-categories" href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></li>';
      }
    ?>
  </ul>
<? 
    // Our variables
    $numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 0;
    $page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;

    query_posts(array(
          'posts_per_page' => $numPosts,
          'paged'          => $page,
          'post_type'      => 'theme_video'
    ));

    // our loop
    if (have_posts()) {
          while (have_posts()){
                the_post();
                get_template_part( 'partials/content', 'browse' );
          }
    }
    wp_reset_query();
  ?>

I'm a bit lost on how to proceed. Nothing I tried seems to sort the loop, and the methods ive used have been outdated by 1-2 years...

Site works well for my first theme, but I do not know how to get this loop to sort by the category you select. Any tips or pointers?

Thanks!

share|improve this question
1  
I strongly recommend against using query_posts, use WP_Query instead –  Tom J Nowell Jun 24 at 22:51
    
Understood. Would I simple replace query_posts with WP_Query ? –  FlannelBeard Jun 25 at 13:50
    
You should take a look at the "You don't know query" slides by Andrew Nacin slideshare.net/andrewnacin/… –  Tom J Nowell Jun 25 at 14:31
    
@TomJNowell Thanks ill check it out now. :) still a bit new to query, loop, etc. –  FlannelBeard Jun 25 at 14:38

3 Answers 3

You have the orderby parameter in your query but you are missing the companion order. This gives ascending/descending rule to the sortation.

WordPress Codex reference

share|improve this answer

I'm having trouble understanding the question, but I'm assuming the code snippet you posted containing query_posts is in a category, archive or index template which is called when you follow on of the links generated by get_category_link (). If so, the query_posts function is overriding the default loop, but you're not including the category to filter by so it's grabbing all posts in that post type.

// Our variables
$numPosts = (isset($_GET['numPosts'])) ? $_GET['numPosts'] : 0;
$page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;

// build query
$args = array(
  'posts_per_page' => $numPosts,
  'paged'          => $page,
  'post_type'      => 'theme_video'
);

// get category ID
$cat_id = get_query_var( 'cat' );

// add category to query if an id is found
if ( $cat_id != '' ) {
  $cat = get_category ( $cat_id );
  $args['category'] = $cat->slug;
}

// go!
query_posts( $args );

On another not I'd suggest looking into using the pre_get_posts hook as opposed to the query_posts function.

share|improve this answer
    
I appreciate your post, and I think we are close. On my theme you have a Browse page which pulls all the videos from the Custom Post Type I am working with. If a user makes 30 categories id like for users to be able to sort the videos using the Category buttons listed at the top. If they click on Horror id like to loop to show all Horror Videos. Can this code achieve that, and how can I link my Category Links to make the loop transition this way? See OP. –  FlannelBeard Jun 25 at 14:37

EDIT: Answer has been found and I am posting it for anyone else who has this issue. If you have better ways of accomplishing this then please post below for the benefit of others.

Place this code in your category.php. It will automatically grab the category from the URL and pass it to the cat => $category_id and update the loop accordingly.

<ul class="sort-by-category">
<?
  $args = array(
        'orderby' => 'name',
        'parent' => 0
  );
  $categories = get_categories( $args );
  foreach ( $categories as $category ) {
    echo '<li><a class="browse-categories" href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></li>';
  }
?>
</ul>

<?
    // Our variables
    $page = (isset($_GET['pageNumber'])) ? $_GET['pageNumber'] : 0;

    // grab slug from url
    $category_id = get_query_var('cat');

    // define args
            $args = array( 
                        'posts_per_page' => '36',
          'paged'          =>  $page,
          'post_type'      => 'theme_video',
          'cat'            =>  $category_id
     );

            // place args in query
            $category_posts = new WP_Query($args);

            // our loop
            if($category_posts->have_posts()) { 
            while($category_posts->have_posts()) { 
                    $category_posts->the_post();
                    get_template_part( 'partials/content', 'browse' );
            }
    } 
    wp_reset_query();
?>
share|improve this answer

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.