How to decrease the number of queries with get_posts and ACF?

Question

In my index.php file, inside the classic WordPress loop, I had to query posts in order to retrieve the ones with a relationship value created with a custom field (managed with ACF plugin).

I managed to do it using the get_posts() function and a foreach loop. But, as I have 300 posts, it slows down the page load time.

So, in this perspective, I was looking for a way to reduce the amount of posts fetched by get_posts(), or the number of queries, using different parameters. But, it seems the parameters I’m using are increasing the number of queries.

This is the code I’m trying to improve :

    <?php
    $artists = get_posts(array(
      'post_type'           => 'post',
      'numberposts'         => '1',
      'meta_query'          => array(
            array(
              'key'       => 'relation', // name of custom field
                'value'   => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
                'compare' => 'LIKE'
            )
      )
    ));

    ?>
    <?php if( $artists ) : ?>
      <?php foreach( $artists as $artist ) : ?>
        <p>
          <a href="<?php echo get_permalink( $artist->ID ); ?>"><?php echo get_the_title( $artist->ID ); ?></a>
        </p>
      <?php endforeach; ?>
    <?php endif; ?>

First of all, I’m using the paramater 'numberposts' => '1', instead of the default 'numberposts' => '5', in order to limit the number of posts and the query time, but i dont see any improvement, and if i use ‘numberposts’ => ‘-1’ (which is not limiting the number of posts), the number of queries decrease and the load time as well. Why ?

Then, if i use this chunk of code instead of the previous one, the number of queries increase but the load time decrease :

<?php
    // trying to limit the number of posts targeting only the one with a specific relationship field
    $ids = get_field('relationship_field_name', false, false);

    $artists = get_posts(array(
      'post_type'           => 'post',
      'numberposts'         => '1',
      'post__in'            => $ids,
      )
    ));

    ?>
<?php if( $artists ) : ?>
  <?php foreach( $artists as $artist ) : ?>
    <p>
      <a href="<?php echo get_permalink( $artist->ID ); ?>"><?php echo get_the_title( $artist->ID ); ?></a>
    </p>
  <?php endforeach; ?>
<?php endif; ?>

EDIT :

The most efficient piece of code I’ve been able to produce is, with (according to Query Monitor plugin) only 616 queries and 0,4775s Database Query Time :

<?php
$ids = get_field('relation', false, false);

$artists = get_posts(array(
  'post_type'     => 'post',
  'numberposts'   => '-1',
  'post__in'      => $ids,
));

?>
<?php if( $artists ) : ?>
  <?php foreach( $artists as $artist ) : ?>
    <p>
      <a href="<?php echo get_permalink( $artist->ID ); ?>"><?php echo get_the_title( $artist->ID ); ?></a>
    </p>
  <?php endforeach; ?>
<?php endif; ?>

If I set the parameter ‘numberposts’ to 1, or if I let to its default value, I obtain 1069 Queries and 0,7291s Database Query Time.

What a Im missing with get_posts() ? How to optimize the efficiency of the code, decrease the number of queries and the load time ?

0
, , PhpDoe 2 years 2020-01-09T08:40:10-05:00 0 Answers 80 views 0

Leave an answer

Browse
Browse