• Hi,

    I’ve created a query to display custom posts by calling them from the category. And the issue is although I want to display 3 different posts in the post section, It only displays 1 same post for 3 each content section.

    Here is the reference screenshot link for the issue.

    
    $args = ( array(
        'category_name'  => 'sidepost',
        'post_status'   => 'publish',
        'posts_per_page' => -1,
    	'order' => 'ASC',
    	'orderby' => 'date',
    ) );
    // the query
    $the_query = new WP_Query( $args ); ?>
     
    
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    // CUSTOM PHP & HTML CODE
    
    <?php endwhile; ?>
     
    <?php wp_reset_postdata(); ?>

    Thanks in advance.

    • This topic was modified 4 years, 11 months ago by kesking7.
    • This topic was modified 4 years, 11 months ago by kesking7.
    • This topic was modified 4 years, 11 months ago by kesking7.
Viewing 1 replies (of 1 total)
  • I would use get_posts instead, and a foreach, like this:

    $args = ( array(
        'category_name'  => 'sidepost',
        'post_status'   => 'publish',
        'posts_per_page' => -1,
    	'order' => 'ASC',
    	'orderby' => 'date',
    ) );
    // the query
    $my_posts = get_posts( $args );
    if( ! empty( $my_posts ) ){
    	$output = '<ul>';
    	foreach ( $my_posts as $p ){
    		$output .= '<li><a href="' . get_permalink( $p->ID ) . '">' 
    		. $p->post_title . '</a></li>';
    		$output .= '<li><a href="' . get_permalink( $p->ID ) . '">' 
    		. $p->post_title . '</a></li>';
    	}
    	$output .= '<ul>';
    }

    You’ll need to modify and add the content based on your requirements, i.e.
    get_the_post_thumbnail()
    the_content()

Viewing 1 replies (of 1 total)

The topic ‘Custom Post Query Error’ is closed to new replies.