0

I have the following category layout:

Resources
    -- Documents

I want to list the posts in the documents category, and have the following code:

$args = array(
        'posts_per_page' => '-1',
        'orderby'          => 'ID',
        'order'            => 'DESC',
        'category-name'    => 'documents'             
        );
$surveys = get_posts($args);

This returns the posts in both documents AND resources categories. I only want those in 'documents'.

I know there is ways to exclude categories by ID but I need to use the category slug as my dev and live databases do not match.

1

I wonder if a single typo is messing things up for you? Please try this:

$args = array(
        'posts_per_page'   => '-1',
        'orderby'          => 'ID',
        'order'            => 'DESC',
        'category_name'    => 'documents'             
        );
$surveys = get_posts($args);

where I replaced category-name with category_name.

I know there is ways to exclude categories by ID but I need to use the category slug as my dev and live databases do not match.

There exists for example the function get_category_by_slug() that you can use if you need to get the term_id from the slug:

if( FALSE !== ( $obj = get_category_by_slug('some-category-slug') ) )
{
    $id = $obj->term_id;
}
  • 1
    Damn! I had looked at that over and over as well. Thanks for pointing that out. – Bazlid Jun 20 '14 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.