• Resolved withinreach

    (@withinreach)


    I am able to grab all posts for a given date (ex: today), but I also then need to sort the returned posts by category ID. The order ID is different from cat name or post title order.

    I created the categories in the specific order I need them to appear, so the IDs (17,18,19,20) are how I wish to order the results by day.

    I can’t seem to find the correct ‘orderby’ parameter.

    This is for a daily news site, so it needs to be automated.

    As I said, I got the ‘get today’s posts’ working, now I need them ordered by my category IDs.

    Help!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hey there, can I see the code you’re using to query the posts? We can probably edit that to do what you want.

    Thread Starter withinreach

    (@withinreach)

    this gets me the posts of the day.

    $today = getdate();
    query_posts(‘year=’ .$today[“year”] .’&monthnum=’ .$today[“mon”] .’&day=’ .$today[“mday”] );

    cats would be 17,18,19,20

    My workaround for now was to duplicate the ‘show post’ code 4 times (yeah, kludge) by appending the &cat=17 to each query.

    thanks folks!

    Something like this: https://wordpress.pastebin.com/2TaXqEEM should work.

    What we do there is first we get the categories ordered by id, ascending:

    $cat_args=array(
      'orderby' => 'ID',
      'order' => 'ASC'
      'include'=>'17,18,19,20',
       );
    $categories=get_categories($cat_args);

    then, we loop trought the categories, calling the posts inside the category that fit your criteria (posts from today, in this case):

    foreach($categories as $category) {
    	$args=array(
        	'showposts' => -1,
          	'category__in' => array($category->term_id),
          	'caller_get_posts'=>1,
          	'year'=>$today["year"],
          	'monthnum='=>$today["mon"],
          	'&day='=>$today["mday"]
        );
        $posts=get_posts($args);

    For each post that fills the criteria, we show it. In the code, the divs names and whatnot are taken from the default theme.

    Hope that helps.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘sorting results by category ID within date’ is closed to new replies.