WordPress.org

Ready to get started?Download WordPress

Forums

Featured image is not being displayed inside custom WP_Query (2 posts)

  1. kab2512
    Member
    Posted 5 months ago #

    I have an issue regarding WP_Query on a page.
    I run a second loop outside of the main loop which looks like this.

    $args = array(
    	'post_type'  => 'personel',
    	'orderby'    => 'title',
    	'order'      => 'ASC',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'team',
    			'field' => 'id',
    			'terms' => array( 5,4 )
    		)
    	)
    );
    $__a = 0;
    $__b = 0;
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) : $query->the_post();
        $term = wp_get_post_terms( $post->ID, 'team', array( 'fields' => 'ids' ) );
    	$term = $term[0];
    	$image = array();
    	$image['id']   = get_post_thumbnail_id( $post->ID );
    	$image['alt']  = get_post_meta( $image['id'], '_wp_attachment_image_alt', true );
    	$image['attr'] = array( 'class' => 'coworker', 'alt' => $image['alt'] );
    	if ( $term == 4 )  {
    		if ( $__a == 0 ) echo '<h3>Lead personal founders</h3>';
    	    echo get_the_post_thumbnail( $post->ID, 'post-thumb-90-120', $image['attr'] );
            echo get_the_title();
            echo get_the_content();
    		$__a += 1;
    	}
    	if ( $term == 5 )  {
    		if ( $__b == 0 ) echo '<h3>Part Time Specialists</h3>';
    	    echo get_the_post_thumbnail( $post->ID, 'post-thumb-90-120', $image['attr'] );
            echo get_the_title();
            echo get_the_content();
    		$__b +=1;
    	}
      edit_post_link( __( 'Edit coworker', 'cphsub' ), '<span class="edit-link clr">', '</span>', $post->ID );
      echo '<div class="devider clr"></div>';
      endwhile;
      wp_reset_postdata();
      else:
       _e( 'Sorry, no posts matched your criteria.' );
      endif;

    What am i doing wrong? The featured images are not being displayed even though every custom post in the query have a featured image?

  2. bcworkz
    Member
    Posted 5 months ago #

    When you use if ( $term == 4 ) for example, $term was assigned the first element of the array returned by wp_get_post_terms(). This is not an integer, it is a term object. Your conditional will never evaluate to true. Assuming 4 is supposed to equate to the term ID, your conditional should be like this:
    if ( $term->term_id == 4 )

Reply

You must log in to post.

About this Topic