【问题标题】:How to Display CPT Meta on Front End through Query如何通过查询在前端显示 CPT Meta
【发布时间】:2023-03-11 03:26:01
【问题描述】:

我在我的网站上使用两种自定义帖子类型:TeamLeague。我正在为 CPT 联赛创建一个页面模板,该模板将显示来自团队的所有“团队”帖子的元数据,其元值与联赛帖子类型的名称相匹配。查询将通过 CPT 'team' 查找元值 '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>';

   $users_confirmed = 0;
    foreach ($team_players as $team_player) { 
        $user = get_user_by( 'email', $team_player['email'] );
        if( $user ){
            if( !$user->first_name && !$user->last_name ){
                continue;
            }
            echo sprintf( "<tr><td>%s</td></tr>", $user->first_name . ' ' . $user->last_name );
            $users_confirmed++;
        }
    }
    if( 0 == $users_confirmed ){
        echo sprintf( "<tr><td>%s</td></tr>", "No confirmed players for this team" );
    }
    echo '</table>';  
  }
} 

else {
  echo '<p>No Teams in this league</p>';
}
/* Restore original Post Data */
wp_reset_postdata();

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    对于您的查询参数。试试这样的:

    $args = array (
        'post_type' => 'team',
        'posts_per_page' => -1,
        'meta_query'  => array(
           'relation' => 'AND',
               array(
                'key'     => 'team_league',
                'value'   => $leaguename,
               ),
        ),
    );
    

    我不认为'post_status' =&gt; 'any' 有效。这也使用了正确的元查询格式。

    【讨论】:

    • 我在这里根据您的建议更新了我的查询参数,并为我的 CPT 添加了特定的帖子状态。
    • 如果我的建议基本有效,你可以投票;)
    【解决方案2】:

    作为参考,以下解决方案可以实现我上面的目标。主要的变化是 args,我必须为我的 CPT 包含自定义帖子状态:

    $leagueid = get_the_ID();
    
    $args = array(
      'post_type'      => 'team',
      'posts_per_page' => - 1,
      'post_status' => array( 'mssl-completed', 'mssl-pending', 'publish' ),
      'meta_query' => array(
         array(
            'key'   => 'team_league',
            'value' => "$leagueid",
         ),
       ),
     );
    

    从这里我能够将从帖子元中提取的信息写入我的自定义页面模板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      • 2023-03-08
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      相关资源
      最近更新 更多