• Hi,

    I’m trying to add a new loop to my index.php page, but can’t figure out how to add only specific categories to the query.

    I’m using the following code to output the last 30 days of posts:

    <?php
      function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
      }
    add_filter('posts_where', 'filter_where');
    query_posts($query_string);
    ?>

    That works fine. But what I’d like to add to that query is the ability to only return posts from a specific category – cat=17.

    I’ve spent the last few hours trying to add an array using ($args) to the query but I don’t really know what I’m doing ??

    Also tried adding the cat parameter to the query post:

    query_posts( $query_string . '&cat=17' );

    But that didn’t work either.

    Any help would be greatly appreciated!

Viewing 6 replies - 1 through 6 (of 6 total)
  • try
    <?php query_posts( array( ‘category_name’ => ‘uncategorized’, ‘order’ => ‘ASC’) ); ?>

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php :

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        query for the home page
        if(is_home()){
          $query->set('cat', 17);
        }
      }
    }
    add_action( 'pre_get_posts', 'my_post_queries' );

    Thread Starter Chris45454

    (@chris45454)

    @nitin_8: Doesn’t work, sorry.

    @keesiemeijer: Thanks, but am getting the following error:

    Parse error: syntax error, unexpected T_FOR in /public_html/wp-content/themes/2012/functions.php on line 961

    I’d like to able to use the query across a number of custom pages, so if there’s a way to just amend the original query to include categories specifically that would be great.

    Moderator keesiemeijer

    (@keesiemeijer)

    Ah sorry, a typo.
    change this:

    query for the home page

    to this:

    // query for the home page

    I’d like to able to use the query across a number of custom pages, so if there’s a way to just amend the original query to include categories specifically that would be great.

    On custom page templates?

    Moderator keesiemeijer

    (@keesiemeijer)

    What you could do it put thefunction in your functions.php:

    function filter_where($where = '') {
        //posts in the last 30 days
        $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
        return $where;
      }

    And put this before every loop where you want 30 days of posts with category 17:

    <?php
    add_filter('posts_where', 'filter_where');
    global $query_string;
    query_posts($query_string .'&cat=17);
    ?>

    And after the loop and pagination:

    <?php
    remove_filter('posts_where', 'filter_where');
    ?>

    Thread Starter Chris45454

    (@chris45454)

    @keesiemeijer: Yeah on custom page templates.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘query Posts: Last 30 days & specific category – 50% working’ is closed to new replies.