In WordPress on the single-player.php page which displays a single profile (from numerous other profiles) with information from around 10 different meta fields.
Without using tags or categories..
What I want to do is at the bottom of the page, have a related section but comparing the other profiles (made from the same custom_type_post) with the current one on the page and if any of the 10 meta fields Values match any other profiles , show them in a slider at the bottom of the page…….but….
Show which Meta fields were matched.
I believe Im currently showing matched profiles…But I m trying to find a way to show how they were matched, by showing The Meta Fields (almost like a tag cloud)
IE: Profile One matched by –> Weight, height and on another profile page
it may be matched by one or two of the other fields
IE: Profile Seven matched by –> Position, height, Team
Here is my code so far.
<!--- RELATED PLAYERS -->
<div class="container" style="margin-bottom:40px;">
<div class="row">
<div class="col-md-12">
<h3 style="margin-top:0;padding-bottom:10px;"><span class="glyphicon glyphicon-random"></span> Similar Players</h3>
<?php
$wp_query = new WP_Query();
$args = array(
'post_type' => 'player',
'posts_per_page' => '-1',
'category_name' => '',
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'player_position',
'value' => $player_position,
'compare' => 'LIKE'
),
array(
'key' => 'player_height',
'value' => $player_height,
'compare' => 'LIKE'
)
)
);
$wp_query->query($args);?>
<div id="carousel-example-generic" class="carousel slide">
<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
// The Loop
//add a class to the first result
$first = true;
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php echo '<div class="item '.($first ? 'active' : '').'">';?>
<div class="col-md-2">
<a href="<?php the_permalink()?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();}?>
</a>
</div>
<div class="col-md-10"> <!-- ///SILDE DATA OUTPUT/ -->
<?php //////////////////// MATCHED META FIELDS HERE /////////////////////// ?>
<?php
$player_name = get_post_meta($post->ID, 'player_name', true);
if (!$player_name =='')
{
echo '<h5>Name: '.'<span style="color:grey;">'.$player_name.'</span></h5>';
}
?>
<?php
$player_tech_char = get_post_meta($post->ID, 'player_tech_char', true);
if (!$player_tech_char =='')
{
echo $player_tech_char;
}
?>
</div> <!-- //////////////END SILDE DATA OUTPUT//////////////////////// -->
</div>
<?php
if ($first) {
$first = false;
}
?>
<?php endwhile;endif;?>
</div>
<!-- Controls -->
<!--
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="icon-prev"></span>
</a>
-->
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
</div>
<!-- END OF OTHER PLAYERS -->
In short
$matchedCustomGetPostMeta = get_post_meta($post->ID, "MATCHED_KEY_FROM_WP_QUERY->have_posts");
Longer Version and Full Code
Function to Create the Custom Post Type and Custom Feilds
<?php
/**
*Creates ADD Player post type
*/
add_action('init', 'player_register_post_type');
function player_register_post_type() {
register_post_type('player', array(
'labels' => array(
'name' => 'Player',
'singular_name' => 'Player',
'add_new' => 'Add New Player Profile',
'edit_item' => 'Edit Player Profile',
'new_item' => 'New Player Profilee',
'view_item' => 'View Player Profile',
'search_items' => 'Search Player Profile',
'not_found' => 'No Player Profiles found',
'not_found_in_trash' => 'No Player Profiles found in Trash'
),
'public' => true,
'show_ui' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'supports' => array(
'title',
'excerpt',
'comments',
'thumbnail',
'editor',
'author',
'page-attributes'
),
'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
));
}
/**
* Add player custom fields
*/
function add_player_meta_boxes() {
add_meta_box("player_contact_meta", "Player Details", "add_player_details_player_meta_box", "player", "normal", "low");
}
function add_player_details_player_meta_box()
{
global $post;
$custom = get_post_custom( $post->ID );
?>
<style>.width99 {width:99%;}</style>
<p>
<label>Player Current Team</label><br />
<input type="text" name="player_current_team" value="<?= @$custom["player_current_team"][0] ?>" class="width99" />
</p>
<p>
<label>Player Name</label><br />
<input type="text" name="player_name" value="<?= @$custom["player_name"][0] ?>" class="width99" />
</p>
<p>
<label>Player Position:</label><br />
<input type="text" name="player_position" value="<?= @$custom["player_position"][0] ?>" class="width99" />
</p>
<p>
<label>Player Age (enter Date of Birth from Date Picker)</label><br />
<input type="text" id="player_dob" name="player_dob" value="<?= @$custom["player_dob"][0] ?>" class="width99" />
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#player_dob').datepicker({
dateFormat : 'dd-mm-yy', defaultDate: -11799
});
});
</script>
</p>
<p>
<label>Player Height:</label><br />
<input type="text" name="player_height" value="<?= @$custom["player_height"][0] ?>" class="width99" />
</p>
<p>
<label>Player Weight:</label><br />
<input type="text" name="player_weight" value="<?= @$custom["player_weight"][0] ?>" class="width99" />
</p>
<p>
<label>Player Laterality:</label><br />
<input type="text" name="player_lat" value="<?= @$custom["player_lat"][0] ?>" class="width99" />
</p>
<p>
<label>Player Domain Legs:</label><br />
<input type="text" name="player_dom_leg" value="<?= @$custom["player_dom_leg"][0] ?>" class="width99" />
</p>
<p>
<label>Player Standing Number:</label><br />
<input type="text" name="player_stand" value="<?= @$custom["player_stand"][0] ?>" class="width99" />
</p>
<p>
<label>Player Body Mass Index:</label><br />
<input type="text" name="player_bmi" value="<?= @$custom["player_bmi"][0] ?>" class="width99" />
</p>
<p>
<label>Player Medical Warnings:</label><br />
<input type="text" name="player_meds" value="<?= @$custom["player_meds"][0] ?>" class="width99" />
</p>
<p>
<label>Player Nationality:</label><br />
<input type="text" name="player_nat" value="<?= @$custom["player_nat"][0] ?>" class="width99" />
</p>
<p>
<label>Player European passport:</label><br />
<input type="text" name="player_eu" value="<?= @$custom["player_eu"][0] ?>" class="width99" />
</p>
<p>
<label>Player Last Team:</label><br />
<input type="text" name="player_last" value="<?= @$custom["player_last"][0] ?>" class="width99" />
</p>
<p>
<label>Technical Characteristics</label><br />
<textarea rows="5" name="player_tech_char" class="width99"><?= @$custom["player_tech_char"][0] ?></textarea>
</p>
<p>
<label>Video Links</label><br />
<textarea rows="5" name="player_vid_link" class="width99"><?= @$custom["player_vid_link"][0] ?></textarea>
</p>
<p>
<label>MEDICALLY AND PSYCHOLOGICAL CHARACTERISTICS</label><br />
<textarea rows="5" name="player_med_char" class="width99"><?= @$custom["player_med_char"][0] ?></textarea>
</p>
<?php
}
/**
* Get Age from DOB ( datepicker)
*/
function get_age( $birthdayIn )
{
$now = new DateTime();
$birthday = new DateTime($birthdayIn);
$interval = $now->diff($birthday);
$playerAge = $interval->format('%y'); // age
return $playerAge;
}
// manipulate $_POST
$birth_date1 = get_age( $_POST["player_dob"] );
$_POST["player_dob"] = $birth_date1;
/**
* Save custom field data when creating/updating posts
*/
function save_player_custom_fields(){
global $post;
if ( $post )
{
update_post_meta($post->ID, "player_current_team", @$_POST["player_current_team"]);
update_post_meta($post->ID, "player_name", @$_POST["player_name"]);
update_post_meta($post->ID, "player_position", @$_POST["player_position"]);
update_post_meta($post->ID, "player_dob", @$_POST["player_dob"]);
update_post_meta($post->ID, "player_height", @$_POST["player_height"]);
update_post_meta($post->ID, "player_weight", @$_POST["player_weight"]);
update_post_meta($post->ID, "player_lat", @$_POST["player_lat"]);
update_post_meta($post->ID, "player_dom_leg", @$_POST["player_dom_leg"]);
update_post_meta($post->ID, "player_stand", @$_POST["player_stand"]);
update_post_meta($post->ID, "player_bmi", @$_POST["player_bmi"]);
update_post_meta($post->ID, "player_meds", @$_POST["player_meds"]);
update_post_meta($post->ID, "player_nat", @$_POST["player_nat"]);
update_post_meta($post->ID, "player_eu", @$_POST["player_eu"]);
update_post_meta($post->ID, "player_last", @$_POST["player_last"]);
update_post_meta($post->ID, "player_tech_char", @$_POST["player_tech_char"]);
update_post_meta($post->ID, "player_tech_char", @$_POST["player_tech_char"]);
update_post_meta($post->ID, "player_vid_link", @$_POST["player_vid_link"]);
update_post_meta($post->ID, "player_med_char", @$_POST["player_med_char"]);
}
}
add_action( 'admin_init', 'add_player_meta_boxes' );
add_action( 'save_post', 'save_player_custom_fields' );
?>
And A shortened Version of the Out Put of a single Custom Post Type Page
<?php
/// Get Meta Key of this Profile Page Currently on
$key_1_value = get_post_meta( get_the_ID(), 'player_position', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo "<h4>This Page Key :" . $key_1_value."</h4>";
}
?>
<div>
<h3 style="margin-top:0;"><span class="glyphicon glyphicon-th-list"></span> INFORMATION </h3>
<?php
$player_name = get_post_meta($post->ID, 'player_name', true);
if (!$player_name =='')
{
echo '<h4>Name: '.'<span style="color:grey;">'.$player_name.'</span></h4>';
}
?>
<?php
$player_position = get_post_meta($post->ID, 'player_position', true);
if (!$player_position =='')
{
echo '<h4>Position: '.'<span style="color:grey;">'.$player_position.'</span></h4>';
}
?>
<?php
$player_dob = get_post_meta($post->ID, 'player_dob', true);
if (!$player_dob =='')
{
echo '<h4>Age: '.'<span style="color:grey;">'.$player_dob.'</span></h4>';
}
?>
<?php
$player_height = get_post_meta($post->ID, 'player_height', true);
if (!$player_height =='')
{
echo '<h4>Height: '.'<span style="color:grey;">'.$player_height.'</span></h4>';
}
?>
<?php
$player_weight = get_post_meta($post->ID, 'player_weight', true);
if (!$player_weight =='')
{
echo '<h4>Weight: '.'<span style="color:grey;">'.$player_weight.'</span></h4>';
}
?>
<?php
$player_lat = get_post_meta($post->ID, 'player_lat', true);
if (!$player_lat =='')
{
echo '<h4>Laterality: '.'<span style="color:grey;">'.$player_lat.'</span></h4>';
}
?>
<?php
$player_dom_leg = get_post_meta($post->ID, 'player_dom_leg', true);
if (!$player_dom_leg =='')
{
echo '<h4>Domain Legs: '.'<span style="color:grey;">'.$player_dom_leg.'</span></h4>';
}
?>
<?php
$player_stand = get_post_meta($post->ID, 'player_stand', true);
if (!$player_stand =='')
{
echo '<h4>Standing Number: '.'<span style="color:grey;">'.$player_stand.'</span></h4>';
}
?>
<?php
$player_bmi = get_post_meta($post->ID, 'player_bmi', true);
if (!$player_bmi =='')
{
echo '<h4>Body Mass Index: '.'<span style="color:grey;">'.$player_bmi.'</span></h4>';
}
?>
<?php
$player_meds = get_post_meta($post->ID, 'player_meds', true);
if (!$player_meds =='')
{
echo '<h4>Medical Warnings: '.'<span style="color:grey;">'.$player_meds.'</span></h4>';
}
?>
<?php
$player_nat = get_post_meta($post->ID, 'player_nat', true);
if (!$player_nat =='')
{
echo '<h4>Nationality: '.'<span style="color:grey;">'.$player_nat.'</span></h4>';
}
?>
<?php
$player_eu = get_post_meta($post->ID, 'player_eu', true);
if (!$player_eu =='')
{
echo '<h4>European passport: '.'<span style="color:grey;">'.$player_eu.'</span></h4>';
}
?>
<?php
$player_last = get_post_meta($post->ID, 'player_last', true);
if (!$player_last =='')
{
echo '<h4>Last Team: '.'<span style="color:grey;">'.$player_last.'</span></h4>';
}
?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<!-- //
//Slider with related profiles
-->
<?php
$wp_query = new WP_Query();
$args = array(
'post_type' => 'player',
'posts_per_page' => '-1',
'category_name' => '',
'orderby' => 'rand',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'player_position',
'value' => $player_position,
'compare' => 'LIKE'
),
array(
'key' => 'player_height',
'value' => $player_height,
'compare' => 'LIKE'
)
)
);
$wp_query->query($args);?>
<?php
// The Loop
$first = true;
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();?>
<?php echo '<div class="item '.($first ? 'active' : '').'">';?>
<div class="col-md-3">
<a href="<?php the_permalink()?>" title="<?php the_title_attribute(); ?>" rel="bookmark">
<?php if ( has_post_thumbnail() ) {the_post_thumbnail();}?>
</a>
</div>
<div class="col-md-3"> <!-- Slide data output -->
<?php
//// COMPARE THE PAGE META KEY WITH OTHER POSTS IF IT MATCHES THEN STYLE
$key_2_value = get_post_meta( get_the_ID(), 'player_position', true );
// check if the custom field has a value
if(( ! empty( $key_2_value ) ) && ($key_2_value == $key_1_value)) {
//echo "<h4>Matched by :" . $key_1_value."</h4>";
$classH6_style ='style="color:deeppink;font-weight:bold;"';
$classSpan_style ='style="color:dodgerblue;font-weight:bold;text-decoration:underline;"';
}
else
{
//echo "No Match For";
$classH6_style='';
$classSpan_style = 'style="color:grey;"';
}
?>
<?php
$player_name = get_post_meta($post->ID, 'player_name', true);
if (!$player_name =='')
{
echo '<h6>Name: '.'<span style="color:grey;">'.$player_name.'</span></h6>';
}
$player_position = get_post_meta($post->ID, 'player_position', true);
if (!$player_position =='')
{
echo '<h6 '.$classH6_style.'>Position: '.'<span '.$classSpan_style.'>'.$player_position.'</span></h6>';
}
$player_dob = get_post_meta($post->ID, 'player_dob', true);
if (!$player_dob =='')
{
echo '<h6>D.O.B: '.'<span style="color:grey;">'.$player_dob.'</span></h6>';
}
$player_height = get_post_meta($post->ID, 'player_height', true);
if (!$player_height =='')
{
echo '<h6>Height: '.'<span style="color:grey;">'.$player_height.'</span></h6>';
}
?>
</div>
</div>
<?php
if ($first) {
$first = false;
}
?>
<?php endwhile;endif;?>
</div>
<?php get_footer(); ?>
This part is what Im trying use to take in multiple keys to match then if on e does then style differently
<?php
//// COMPARE THE PAGE META KEY WITH OTHER POSTS IF IT MATCHES THEN STYLE
$key_2_value = get_post_meta( get_the_ID(), 'player_position', true );
// check if the custom field has a value
if(( ! empty( $key_2_value ) ) && ($key_2_value == $key_1_value)) {
//echo "<h4>Matched by :" . $key_1_value."</h4>";
$classH6_style ='style="color:deeppink;font-weight:bold;"';
$classSpan_style ='style="color:dodgerblue;font-weight:bold;text-decoration:underline;"';
}
else
{
//echo "No Match For";
$classH6_style='';
$classSpan_style = 'style="color:grey;"';
}
?>
Let me see if I understand your question correctly:
You have the meta values of the player this page belongs to. Then in the loop you have the corresponding meta value of each matched player. All you need to do is compare them:
If you have a lot of meta values you should avoid writing a lot of if statements, so better use this:
You can then use this array e.g. to set classes on the element or whatever you need to do.