WordPress.org

Support

Support » How-To and Troubleshooting » Query Posts syntax

Query Posts syntax

  • Hi,

    In the bought out theme it is like

    if ( is_page() ) {
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        query_posts( 'post_type=post&paged=' . $paged );
    }

    I want to modify the above code to include only category “Collaborations” and not all posts to be displayed.

    How to modify the code?

    Thank you,

Viewing 4 replies - 1 through 4 (of 4 total)
  • The code in the bought out theme isn’t using parameterized queries, and could be susceptible to SQL injection:

    So to address that and answer your question, change:

    if ( is_page() ) {
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        query_posts( 'post_type=post&paged=' . $paged );
    }

    to

    if ( is_page() ) {
        $paged = get_query_var('paged') ? get_query_var('paged') : 1;
        query_posts( array ( 'post_type' => 'post', 'category_name' => 'Collaborations', 'posts_per_page' => 5, 'paged' => $paged ) );
    }

    Big caveat, I wrote this in the forum text area (I haven’t tried running it), so I’ll make sure it’s valid when I get back in front of my work computer.

    Sorry ; It displays other post categories also (including uncategorized).

    OK, looks like you need to use the category slug:

    'category_name' => 'my-category-slug'

    or category id:

    'cat' => 22

    Sorry about that…

    Sorry, nothing works in this bought out theme.

    The theme author suggested to make the menu (appearance -> menus) to choose the category “Collaboration” as the menu item and it worked.

    http://test.skinnycreative.com/projects/aob/category/collaborations-cat/

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query Posts syntax’ is closed to new replies.
Skip to toolbar