Support » Fixing WordPress » Adding specific category into an existing query

  • Resolved JMoe

    (@jmoe)


    Here is the code to show the “Latest News” posts on my home page.

    <?php if ( of_get_option(‘gg_headlines’) ) {

    $query = new WP_Query( );
    $post_number = of_get_option(‘gg_headlines’);
    $query->query(‘posts_per_page=’ . $post_number);
    while ($query->have_posts()) : $query->the_post();
    ?>

    I would like to have my home page only show category 4 (Latest News) and not all categories on my blog. How do I go about adding cat=4 in this code?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Give this a shot.

    $query->query('posts_per_page=' . $post_number . '&cat=4');
    Thread Starter JMoe

    (@jmoe)

    Yes! This works perfectly – thank you so much, Andrew. Such a simple solution but I’ve searched on this for hours and couldn’t figure it out 😮

    Sure thing, check out the documentation for WP_Query too, I still refer to it constantly.

    Please mark as resolved and happy coding!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding specific category into an existing query’ is closed to new replies.