I want to create function for compare custom field values of CPT Posts with default posts CF Value. As of now I implemented below code in template file which is working fine, but I want to create function for same task. Function are fast so that's why I want to switch. Anyone has experience to make things faster, answers are welcomed here.
<?php
$posts = get_posts(array(
'numberposts' => 1,
'post_type' => array('company'),
'post_status' => 'publish',
'meta_query' => array(
array(
'relation' => 'OR',
array(
'key' => 'Location',
'value' => get_post_meta(get_the_ID(), 'Location',true),
'compare' => 'IN'
),
array(
'key' => 'Company',
'value' => get_post_meta(get_the_ID(), 'Company',true),
'compare' => 'IN'
)),
array(
'relation' => 'OR',
array(
'key' => '_wp_page_template',
'value' => 'location.php',
'compare' => 'IN'
),
array(
'key' => '_wp_page_template',
'value' => 'company.php',
'compare' => 'IN'
),
) ) ));
if($posts) {
foreach($posts as $post) {
?>
<a href="<?php the_permalink(); ?>"><?php echo get_post_meta(get_the_ID(),
'Location',true); ?></a>
<?php
} }
else {
?>
<?php
echo get_post_meta(get_the_ID(), 'Company',true);
}
?>
<?php wp_reset_query(); ?>