最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - Display featured image from one CPT within another CPT query

programmeradmin1浏览0评论

I have a WP_Query for a CPT of 'fixtures' that has a relationship with another CPT of 'fixtures_teams'. I want to pull in the featured image from the relative team in fixtures_teams CPT.

$query = new WP_Query( array(
  'post_type' => 'fixtures',
  'posts_per_page' => -1,
  'meta_key' => 'date',
  'orderby' => 'meta_value_num',
  'order' => 'ASC',
));

if ( $query->have_posts() ) { ?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <p class="team-name"><?php the_field('team_home'); ?></p>

***<!-- here I want to get the featured-image from 'fixtures_teams' where the CPT matches this 'team_home' -->***

  <p><span class="versus">V</span></p>

***<!-- here I want to get the featured-image from 'fixtures_teams' where the CPT matches this 'team_away' -->***

  <p class="team-name"><?php the_field('team_away'); ?></p>

<?php endwhile;
wp_reset_postdata(); ?>

Thanks in advance :)

I have a WP_Query for a CPT of 'fixtures' that has a relationship with another CPT of 'fixtures_teams'. I want to pull in the featured image from the relative team in fixtures_teams CPT.

$query = new WP_Query( array(
  'post_type' => 'fixtures',
  'posts_per_page' => -1,
  'meta_key' => 'date',
  'orderby' => 'meta_value_num',
  'order' => 'ASC',
));

if ( $query->have_posts() ) { ?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <p class="team-name"><?php the_field('team_home'); ?></p>

***<!-- here I want to get the featured-image from 'fixtures_teams' where the CPT matches this 'team_home' -->***

  <p><span class="versus">V</span></p>

***<!-- here I want to get the featured-image from 'fixtures_teams' where the CPT matches this 'team_away' -->***

  <p class="team-name"><?php the_field('team_away'); ?></p>

<?php endwhile;
wp_reset_postdata(); ?>

Thanks in advance :)

Share Improve this question edited Aug 30, 2019 at 11:31 seanuk asked Aug 30, 2019 at 9:52 seanukseanuk 311 silver badge2 bronze badges 2
  • Is fixtures_teams a meta key? Taxonomy term? Or is it the actual custom post type you want to retrieve from, if so, what is the value of team_home is it a meta key/meta value? Taxonomy term? etc... – Adam Commented Aug 30, 2019 at 11:58
  • fixtures_teams is a CPT, using post title and featured image only. – seanuk Commented Aug 31, 2019 at 8:35
Add a comment  | 

1 Answer 1

Reset to default 0

The following is an example of how you could achieve this (I am assuming the_field is a result of Advanced Custom Fields)...

Please note that it is incredibly inefficient, there are other ways to do this that may improve performance and reduce the need for repetitive code.

<?php

$query = new WP_Query( array(
  'post_type' => 'fixtures',
  'posts_per_page' => -1,
  'meta_key' => 'date',
  'orderby' => 'meta_value_num',
  'order' => 'ASC',
));

if ( $query->have_posts() ) { ?>

<?php while ( $query->have_posts() ) : $query->the_post(); ?>

  <p class="team-name"><?php the_field('team_home'); ?></p>

  <?php

    $team_home = get_posts(array(
        'post_type' => 'fixtures_teams',
        'posts_per_page' => 1,
        'meta_query' => array(
            array(
                'key'     => 'team_home',
                'value'   => get_field('team_home'), //assuming ACF here so change the_field to get_field
            ),
        ),
    ));

    $team_away = get_posts(array(
        'post_type' => 'fixtures_teams',
        'posts_per_page' => 1,
        'meta_query' => array(
            array(
                'key'     => 'team_away',
                'value'   => get_field('team_away'), //assuming ACF here so change the_field to get_field
            ),
        ),
    ));

    // replace "large" with the particular size you want
    // see https://developer.wordpress/reference/functions/get_the_post_thumbnail/
    $image_home = get_the_post_thumbnail($team_home, 'large' );
    $image_away = get_the_post_thumbnail($team_away, 'large' );

  ?>

  <!-- print your image here and markup as desired -->
  <?php echo $image_home; ?>

  <p><span class="versus">V</span></p>

  <!-- print your image here and markup as desired -->
  <?php echo $image_away; ?>

  <p class="team-name"><?php the_field('team_away'); ?></p>

<?php endwhile; wp_reset_postdata(); ?>
发布评论

评论列表(0)

  1. 暂无评论