I want to display the all the posts that are inside a certain subcategory

I want to display the all the posts that are inside a certain subcategory in a specific order that I decide (this is because the category contains a list of tutorials and I want to decide how to list them).

How can I do that?

Read More

Is there any plugin that I can use? I just want to be able to set the order when displaying that category.

Thanks.

Related posts

Leave a Reply

2 comments

  1. You can use the get_posts() function and create a custom template file for that! This way you can take advantage of all the arguments that function makes use of.
    For example, to show all posts from a category that has id=47 you would do the following:

    <ul>
    <?php
    global $post;
    $args = array( 'numberposts' => 999, 'category' => 47, 'orderby' => 'post_date', 'order' => 'DESC',);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    

    This will echo the titles of 999 posts (each title linking to the post itself) and it will format them as a list with the <ul>, <li> that we used.
    Of course you’ll have to customize that to suit your use-case but this is a working principal.

    I hope that helps!

    Cheers,
    Ari.

  2. post__in will preserve the order of the values you give it.

    ‘post_in’ – Preserve post ID order given in the post_in array
    (available with Version 3.5).

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Assuming that “3,5,8,4” are post IDs, something like the following should work:

    $query = new WP_Query( 
      array ( 
        'post__in' => array(3,5,8,4)
      ) 
    );
    

    Of course, you will need to add other parameters that you might want.

    Note: It is cat not category, but it doesn’t matter since post__in will take care of everything anyway. That is, if you are picking the posts to display by ID, as it sounds like you want to do, you don’t really need any other parameters.