0

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(); ?>
2
  • First, congrats on creating this complex query. But if I understand you right — turning this into a function wouldn't make it faster, since it'd be running the same query. Functions would let you re-use the same code elsewhere, however. Are you having any problems with performance? Sep 20, 2022 at 5:57
  • Yes, I have large db table size which have >10 Million custom fields record and with this code the timing for load page taking long. As you have written this is a complex query and require implementing at multiple locations, so I want the function to reduce size of source code (template) and want to check will it improve performance. If you have any other option to tackle this problem, let me know.
    – Puneet
    Sep 20, 2022 at 11:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.