• Resolved dbued

    (@dbued)


    I’m really getting mad down here…I am trying to output the latest 5 posts of my 4 CPT with a specific category but still I don’t get any output.

    Here is what I’m doing:

    <?php 
    
    	global $wp_query; 
    
    	$wp_query = new WP_Query( 
    
    		array( 
    
    			'post_type' => array( 'wuelfrath' , 'mettmann', 'haan', 'neanderland' ),
    			'showposts' => 5,
    			'category_name' => array( 'kurz-notiert-neanderland' , 'kurz-notiert-mettmann', 'kurz-notiert-haan', 'kurz-notiert-wuelfrath' ) 
    
    		)
    
    	);
    
    	if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		<article>
    
    			<?php if (get_comments_number()==0) { } else { ?>
    			    <div class="comment-bubble"><?php $id = get_the_ID(); $comments_count = wp_count_comments( $id ); echo $comments_count->approved; ?></div>
    			<?php } ?>
    
    			<span class="stream-shortys-date-time"><?php the_time('D. d.m.y'); ?></span>
    
    			<span class="stream-shortys-city"><?php $post_type = get_post_type_object( get_post_type($post) ); echo $post_type->label ; ?></span>
    
    			<a href="<?php the_permalink(); ?>" rel="bookmark"><h3 class="stream-shortys-headline"><?php the_title(); ?></h3></a>
    
    		</article>
    
    		<hr class="divider-smaller">
    
    	<?php 
    
    	endwhile; endif; wp_reset_query();
    
     ?>

    Am I doing things wrong?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi,

    I’m sorry you’re having trouble.

    Are you trying to have paginated pages of five posts on each page, or only display the five most recent posts from those categories for those post types?

    We can help once we understand more what you’re trying to do.

    Also, you might find helpful a tool like https://generatewp.com/wp_query/ to double check your queries.

    Thread Starter dbued

    (@dbued)

    We are a newssite and we’re reporting about 4 different cities in our region. each city has different categories, like main news, short news, sport news…to be more specific with the categories each city has it’s own category connected with the town name.

    For example:

    CPT Wülfrath = Categories: “Main news Wülfrath” / “Short news Wülfrath” / “Sport news Wülfrath”

    CPT Mettmann = Categories: “Main news Mettmann” / “Short news Mettmann” / “Sport news Mettmann”

    and so forth…

    What I’m trying to do is to get the latest postings of ALL 4 cities (so 4 different CPT) of a specific category like main news. So my thought was, to Query postings of all CPT and search for the 5 recent postings with the category “Short news Wülfrath” and “Short news Mettmann” and so forth…in the end I want to receive the recent postings of 4 CPT and 4 different categories.

    If you want to have a look: https://www.taeglich.me – it is in German, but I think you can see what I’m trying to achieve. Right now I’m working with global $wp_query and query posts…

    Thread Starter dbued

    (@dbued)

    Here you have an example of the register of a CPT:

    add_action('init', 'cptui_register_my_cpt_wuelfrath');
    	function cptui_register_my_cpt_wuelfrath() {
    		register_post_type('wuelfrath', array(
    			'label' => 'W&uuml;lfrath',
    			'description' => '',
    			'public' => true,
    			'show_ui' => true,
    			'show_in_menu' => true,
    			'capability_type' => 'post',
    			'map_meta_cap' => true,
    			'hierarchical' => false,
    			'rewrite' => array('slug' => 'wuelfrath', 'with_front' => 1),
    			'query_var' => true,
    			'menu_position' => '5',
    			'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
    			'yarpp_support' => true,
    			'taxonomies' => array('category','post_tag'),
    			'labels' => array (
    				'name' => 'Wuelfrath',
    				'singular_name' => 'Wuelfrath',
    				'menu_name' => 'Wülfrath',
    				'add_new' => 'Neuer Artikel',
    				'add_new_item' => 'Neuen Artikel hinzufügen',
    				'edit' => 'Bearbeiten',
    				'edit_item' => 'Artikel bearbeiten',
    				'new_item' => 'Neuer Artikel',
    				'view' => 'Ansehen',
    				'view_item' => 'Artikel ansehen',
    				'search_items' => 'Artikel suchen',
    				'not_found' => 'Keinen Artikel gefunden',
    				'not_found_in_trash' => 'No Wuelfrath Found in Trash',
    				'parent' => 'Parent Wuelfrath',
    				)
    			)
    		);
    	}
    Moderator keesiemeijer

    (@keesiemeijer)

    Do the posts from the post types display if you remove the category_name argument?

    Change showposts to posts_per_page.

    Another thing to try is querying without using the global $wp_query variable with a WP_Query.

    <?php
    
    $the_query = new WP_Query(
    	array(
    		'post_type' => array( 'wuelfrath' , 'mettmann', 'haan', 'neanderland' ),
    		'posts_per_page' => 5,
    		'category_name' => array( 'kurz-notiert-neanderland' , 'kurz-notiert-mettmann', 'kurz-notiert-haan', 'kurz-notiert-wuelfrath' )
    	)
    );
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	<!-- your code here -->
    	<?php endwhile; ?>
    
    	<?php wp_reset_postdata(); ?>
    <?php endif; ?>

    https://codex.www.ads-software.com/Function_Reference/WP_Query

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_Query problems with custom post type’ is closed to new replies.