如何通过查询在前端显示CPT元

我在网站上使用两种自定义帖子类型:Team和League。我正在为CPT联赛创建一个页面模板,该模板将显示团队所有“团队”帖子中的元数据,其元值与联赛职位类型的名称相匹配。该查询将在CPT“团队”中查找元值“ team_league”。“ team_league”中的值将与“ league”类型的职位ID之一匹配。然后,我想在“联赛”页面上打印每个匹配的“团队”以及团队帖子中的一些元数据。


如果仅打印一个简单的查询名称,而该名称仅包含查询中找到的帖子名称,则可以成功完成此操作。下面的示例成功查询并打印每个“团队”的职位名称,并带有与“联赛”标题匹配的元值。但是,我需要更复杂的东西。我想从“团队”帖子中提取元数据,并打印一个包含“团队” CPT名称的表格。


有效的简化查询:


if ( $the_query->have_posts() ) {

  echo '<ul>';

  while ( $the_query->have_posts() ) {

    $the_query->the_post();

    echo '<li>' . get_the_title() . '</li>';

  }

  echo '</ul>';

所需查询,当前不起作用:


<?php

/*

Template Name: League Layout

Template Post Type: league

*/


$leaguename = get_the_ID();


$args = array(

    'meta_key' => 'team_league',

    'meta_value' => $leaguename,

    'post_type' => 'team',

    'post_status' => 'any',

    'posts_per_page' => -1

);


$the_query = new WP_Query( $args );


// The Loop

if ( $the_query->have_posts() ) {


  while ( $the_query->have_posts() ) {

    $the_query->the_post();


    //Get the team data

    $post_status = get_post_status();

    $team_name = get_the_title();

    $captain_id = get_post_meta(get_the_ID(), 'captain_user_id', true);

    $team_captain = get_user_by( 'ID', $captain_id );

    $team_league = get_post(get_post_meta(get_the_ID(), 'team_league', true));

    $team_players = get_post_meta(get_the_ID(), 'team_players_emails', true);

    $team_paid = get_post_meta(get_the_ID(), 'current_paid_amount', true);

    $team_fee = get_post_meta(get_the_ID(), 'payment_product_price', true);

    $team_balance = round( $team_fee - $team_paid , 2);


   echo '<p>Team Captain: <b>' . $team_captain->first_name . ' ' . $team_captain->last_name; . '</b></p>';

   echo '<p>Team Balance: <b>$' . $team_balance; . '</b></p>';

   echo '<table>';

   echo '<tr>';

   echo '<th>Team Roster</th>';

   echo '</tr>';


慕妹3242003
浏览 152回答 2
2回答

哆啦的时光机

为您的查询Args。尝试这样的事情:$args = array (&nbsp; &nbsp; 'post_type' => 'team',&nbsp; &nbsp; 'posts_per_page' => -1,&nbsp; &nbsp; 'meta_query'&nbsp; => array(&nbsp; &nbsp; &nbsp; &nbsp;'relation' => 'AND',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'key'&nbsp; &nbsp; &nbsp;=> 'team_league',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'value'&nbsp; &nbsp;=> $leaguename,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;),&nbsp; &nbsp; ),);我认为没有用'post_status' => 'any'。这也使用正确的元查询格式。

人到中年有点甜

作为参考,以下解决方案可以实现我的目标。主要更改是args,我必须在其中包含CPT的自定义帖子状态:$leagueid = get_the_ID();$args = array(&nbsp; 'post_type'&nbsp; &nbsp; &nbsp; => 'team',&nbsp; 'posts_per_page' => - 1,&nbsp; 'post_status' => array( 'mssl-completed', 'mssl-pending', 'publish' ),&nbsp; 'meta_query' => array(&nbsp; &nbsp; &nbsp;array(&nbsp; &nbsp; &nbsp; &nbsp; 'key'&nbsp; &nbsp;=> 'team_league',&nbsp; &nbsp; &nbsp; &nbsp; 'value' => "$leagueid",&nbsp; &nbsp; &nbsp;),&nbsp; &nbsp;),&nbsp;);从这里,我能够将从post meta提取的信息写到我的自定义页面模板中。
打开App,查看更多内容
随时随地看视频慕课网APP