1

I have this custom post:

add_action('init', 'eveniment_register');

function eveniment_register() {
    $args = array(
        'label' => __('Evenimente'),
        'singular_label' => __('Eveniment'),
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => true,
        'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies'    => array('category','post_tag','post_thumbnail')

    );

    register_post_type( 'eveniment' , $args );
}

And all those custom-posts have a category with ID=5.

In category-5.php I want to show all custom posts from that category.

How I did it with normal posts with:

while (have_posts()) : the_post(); 

doesn't work with custom posts.

Any ideas? Thanks!

1

Don't change the main query. You can simply achieve what you want by using pre_get_posts.Fall back to the default loop in category-5.php and add the following to your functions.php

function include_category_cpt( $query ) {
    if ( !is_admin() && $query->is_category( '5' ) && $query->is_main_query() ) {
        $query->set( 'post_type', 'eveniment' );
        $query->set( 'post_status', 'publish' );
        $query->set( 'posts_per_page', '-1' );
        $query->set( 'ignore_sticky_posts', '1' );

    }
}
add_action( 'pre_get_posts', 'include_category_cpt' );

Also, please note, caller_get_posts has long time been depreciated and as replaced with posts_per_page

References

1

I solved it like that:

<?php
$type = 'eveniment';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1);

$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

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.