如何显示具有特定元密钥的CPT?

时间:2016-10-08 作者:bdtheme

我试图显示来自CPT的帖子,但它没有显示任何记录。我想显示具有meta\\u键的帖子:match_details_competition_namematch_details_session_name.空心化是我的准则。

  function my_custom_leauge_table(){

     $args = array(
        \'posts_per_page\' => -1,
        \'post_type\' => \'football_fixture\',
        \'post_status\' => \'publish\',
         // meta query
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'match_details_competition_name\',
            \'value\'   => \'\',
            \'compare\' => \'IN\'
        ),
        array(
            \'key\'     => \'match_details_session_name\',
            \'value\'   => \'\',
            \'compare\' => \'IN\'
        ),

    )

    );  

    $my_query = null;
    $my_query = new WP_Query($args);


    $temp_teams_home=array();
    $temp_teams_away=array();
    $winning_team=array();
    $losing_team=array();
    $drawn_team=array();
    $league_ids=array();
    $season_ids=array();
    $total_goals=[];
    //$totalrows_count=0;

    while ($my_query->have_posts()) : $my_query->the_post();




        $temp_teams_home[] = get_post_meta( get_the_ID(), \'match_details_home_team\', true );
        $temp_teams_away[] = get_post_meta( get_the_ID(), \'match_details_away_team\', true );
        $home_team = get_post_meta( get_the_ID(), \'match_details_home_team\', true );
        $away_team = get_post_meta( get_the_ID(), \'match_details_away_team\', true );

        $home_goals = get_post_meta( get_the_ID(), \'match_details_goals_scored_home_team\', true );
        $away_goals = get_post_meta(get_the_ID(), \'match_details_goals_scored_away_team\', true );
        //$competition_name = get_post_meta( get_the_ID(), \'match_details_competition_name\', true );


        if($home_goals > $away_goals){
            $winning_team[]=$home_team;
            $losing_team[]=$away_team;          
        } 
        if($home_goals < $away_goals){
            $winning_team[]=$away_team;
            $losing_team[]=$home_team;
        } 
        if($home_goals == $away_goals){
            $drawn_team[]=$home_team;
            $drawn_team[]=$away_team;
        }           

        $total_goals_scored[]=array($home_team=>$home_goals,$away_team=>$away_goals);
        $total_goals_condeded[]=array($home_team=>$away_goals,$away_team=>$home_goals);
        //$totalrows_count++;


    endwhile;


    $total_matches_played = array_count_values(array_merge($temp_teams_home,$temp_teams_away));
    $team_wins = array_count_values($winning_team);
    $team_loses = array_count_values($losing_team);
    $team_drawn = array_count_values($drawn_team);  
    $total_goals_scored_by_teams=get_single_team_total2($total_goals_scored);
    $total_goals_condeded_by_teams=get_single_team_total2($total_goals_condeded);

    $total_points=0;
    $team_points=[];
    $team_points2=[];
    $team_points3=[];
    foreach($team_wins as $tw_key=>$tw_val){
        $team_points[$tw_key]=$tw_val*3;
    }

    foreach($team_drawn as $tw_key=>$tw_val){
        $team_points2[$tw_key]=$tw_val*1;
    }

    $total_points_final = array();
    $total_points_final2 = array();
    foreach (array_keys($team_points + $team_points2) as $key) {
        $total_points_final[$key] = (isset($team_points[$key]) ? $team_points[$key] : 0) + (isset($team_points2[$key]) ? $team_points2[$key] : 0);
    }

    foreach($team_loses as $tw_key=>$tw_val){
        $team_points3[$tw_key]=$tw_val*0;
    }

    foreach (array_keys($total_points_final + $team_points3) as $key) {
        $total_points_final2[$key] = (isset($total_points_final[$key]) ? $total_points_final[$key] : 0) + (isset($team_points3[$key]) ? $team_points3[$key] : 0);
    }
    $result=generate_table_details2($total_matches_played,$team_wins,$team_loses,$team_drawn,$total_goals_scored_by_teams,$total_goals_condeded_by_teams,$total_points_final2);

    if($totalrows_count>0):
    $content.=\'<table class="leaguetable">\';
        $content.=\'<thead class="leaguetable-head">\';
            $content.=\'<tr>\';
                $content.=\'<th class="pos-head" data-toggle="tooltip" data-placement="top" title="Position">Rank</th>\';
                $content.=\'<th class="team-head">Team</th>\';
                $content.=\'<th class="played-head">GP</th>\';
                $content.=\'<th class="own-head">W</th>\';
                $content.=\'<th class="drawn-head">D</th>\';
                $content.=\'<th class="lost-head">L</th>\';
                $content.=\'<th class="gf-head">GF</th>\';
                $content.=\'<th class="ga-head">GA</th>\';
                $content.=\'<th class="gd-head">GD</th>\';
                $content.=\'<th class="points-head">Pts</th>\';
                $content.=\'<th class="qualification-head">Qualification or relegation</th>\';
            $content.=\'</tr>\';
        $content.=\'</thead>\';
        $content.=\'<tbody class="table-body">\';
        for($i=0;$i<sizeof($result[\'team_list\']);$i++){
            $content.=\'<tr>\'; 


                $win_res;
                if($result[\'total_wins\'][$result[\'team_list\'][$i]]!=\'\'){
                    $win_res=$result[\'total_wins\'][$result[\'team_list\'][$i]];
                } else {
                    $win_res=0;
                }

                $draw_res;
                if($result[\'team_drawn\'][$result[\'team_list\'][$i]]!=\'\'){
                    $draw_res=$result[\'team_drawn\'][$result[\'team_list\'][$i]];
                } else {
                    $draw_res=0;
                }

                $loss_res;
                if($result[\'total_loses\'][$result[\'team_list\'][$i]]!=\'\'){
                    $loss_res=$result[\'total_loses\'][$result[\'team_list\'][$i]];
                } else {
                    $loss_res=0;
                }

                $gf;
                if($result[\'total_goals_for\'][$result[\'team_list\'][$i]]!=\'\'){
                    $gf=$result[\'total_goals_for\'][$result[\'team_list\'][$i]];
                } else {
                    $gf=0;
                }

                $ga;
                if($result[\'total_goals_against\'][$result[\'team_list\'][$i]]!=\'\'){
                    $ga=$result[\'total_goals_against\'][$result[\'team_list\'][$i]];
                } else {
                    $ga=0;
                }

                $tot_points;
                if($result[\'team_points\'][$result[\'team_list\'][$i]]!=\'\'){
                    $tot_points=$result[\'team_points\'][$result[\'team_list\'][$i]];
                } else {
                    $tot_points=0;
                }

                $tot_points;
                if($result[\'team_points\'][$result[\'team_list\'][$i]]!=\'\'){
                    $tot_points=$result[\'team_points\'][$result[\'team_list\'][$i]];
                } else {
                    $tot_points=0;
                }


                $content.=\'<td class="team-pos" >\'.$result[\'team_postiion\'][$result[\'team_list\'][$i]].\'</td>\';
                $content.=\'<td class="team-logo-name"><img src="\'.get_team_image_from_title2($result[\'team_list\'][$i]).\'"  style="height:30px; width:30px;"/>\'.$result[\'team_list\'][$i].\'</td>\';
                $content.=\'<td class="team-played">\'.$result[\'total_matches_played\'][$result[\'team_list\'][$i]].\'</td>\';
                $content.=\'<td class="team-win">\'.$win_res.\'</td>\';
                $content.=\'<td class="team-draw">\'.$draw_res.\'</td>\';
                $content.=\'<td class="team-loss">\'.$loss_res.\'</td>\';
                $content.=\'<td class="team-gf">\'.$gf.\'</td>\';
                $content.=\'<td class="team-ga">\'.$ga.\'</td>\';
                $content.=\'<td class="team-gd">\'.$gd.\'</td>\';
                $content.=\'<td class="team-points">\'.$tot_points.\'</td>\';
            $content.=\'</tr>\';
        }
        $content.=\'</tbody>\';
    $content.=\'</table>\';
    else:
        $content=\'No records found\';
    endif;
    echo $content;

    die();
}

1 个回复
最合适的回答,由SO网友:alxndrbauer 整理而成

如果您只想获得具有meta_key, 然后你可以删除valuecompare 它应该会起作用。

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {