-2

I have created a custom post types in function.php file.

I has created 4 categories on Service menu section, aa, bb, cc and dd, I try to get a latest 5 post for specific category but I can archive this.

this is my query post:

query_posts(array('showposts' => 5, 'post_type' => array('servicios'), 'cat' => 14));

14 is the id for category aa for example, I has tried Tag_ID, category_name and term instead cat and not work. What's wrong in my query?

UPDATE:

These are my argument right now:

$args = array(
  'paged' => 1,
  'posts_per_page' => '5',
  'offset' => 0,
  'post_status' => 'publish',
  'ignore_sticky_posts' => 0,
  'orderby' => 'date',
  'order' => 'DESC',
  'post_type' => array (
                    'servicios' => 'servicios',
                  ),
   'cat' => '15'
);

this is my generated sql query of wp_query:

SELECT SQL_CALC_FOUND_ROWS mt24xi_posts.ID FROM mt24xi_posts 
    INNER JOIN mt24xi_term_relationships ON (mt24xi_posts.ID = mt24xi_term_relationships.object_id) WHERE 1=1 
    AND ( mt24xi_term_relationships.term_taxonomy_id IN (12) ) 
    AND mt24xi_posts.post_type IN ('post', 'page') 
    AND (mt24xi_posts.post_status = 'publish' OR mt24xi_posts.post_author = 1 
    AND mt24xi_posts.post_status = 'private') 
    GROUP BY mt24xi_posts.ID ORDER BY mt24xi_posts.post_date DESC LIMIT 0, 5

If I remove "AND mt24xi_posts.post_type IN ('post', 'page')" the query work fine

0
1

After two days with this problem have resolved the problem, I fix it adding tax_query parameter with my taxonomy name and slug of the category requested:

$args = array(
  'post_type' => array (
                    'servicios' => 'servicios',
                  ),
   'tax_query' => array(
        array(
          'taxonomy' => 'category',
          'field' => 'slug',
          'terms' => 'dgit'
        )
    )
);
0
0

Don't use query_posts

Create a new WP_Query or use pre_get_posts to modify an existing query.

$args = array(
    'post_type' => 'servicios',
    'category_name' => 'category-name'
);
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Swap out category-name with the name of your category.

5
  • Doesn't work, If I use your query without category_name parameter (category slug) the query show all post for "servicios CPT", but when I set the category name in the query nothing is displayed. Ex: 'category_name' => 'dgit' where dgit is my category slug – xzegga Jul 14 '15 at 16:08
  • this is my generated sql query of wp_query: SELECT SQL_CALC_FOUND_ROWS mt24xi_posts.ID FROM mt24xi_posts INNER JOIN mt24xi_term_relationships ON (mt24xi_posts.ID = mt24xi_term_relationships.object_id) WHERE 1=1 AND ( mt24xi_term_relationships.term_taxonomy_id IN (14) ) AND mt24xi_posts.post_type IN ('post', 'page') AND (mt24xi_posts.post_status = 'publish' OR mt24xi_posts.post_author = 1 AND mt24xi_posts.post_status = 'private') GROUP BY mt24xi_posts.ID ORDER BY mt24xi_posts.post_date DESC LIMIT 0, 5 If I remove AND mt24xi_posts.post_type IN ('post', 'page') work fine :S – xzegga Jul 14 '15 at 16:48
  • @xzegga Code in comments is unreadable. – Pieter Goosen Jul 14 '15 at 17:30
  • @PieterGoosen I have updated the post with the code, thanks! – xzegga Jul 14 '15 at 17:35
  • @xzegga Works as i tested it locally. – Brad Dalton Jul 14 '15 at 23:35

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.