I have a wordpress page template, where I'm loading custom posts.
The code for fetching these custom posts looks as follows:
template-parts/content-page.php
<article id="songs">
<?php get_template_part( 'partials/content/custom', 'songs' ); ?>
</article>
partials/content/custom/songs.php
$args = array(
'posts_per_page' => 0,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => $orderBy,
'order' => $order,
'include' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'custom_song',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$songs_array = get_posts( $args );
if (count($songs_array) > 0){
?>
<ul>
<?php
foreach ($songs_array as $mysong){
set_query_var( 'mysong', $mysong);
get_template_part( 'partials/content/custom', 'normal' );
}
?>
</ul>
<?php
}
?>
The problem is that there are over 2000 records. And I want all of them to be loaded at once without any pagination. The above code works and it does load all the posts, but the page is slow because of this query.
Can you please help me how I can optimize this and make the load faster? Is there a way I can load this asynchronously? So that I can show a loading icon in this part of the page till the posts are loaded?
set_query_var
? If the goal is to make the current post available to the template, wouldn't it make more sense to usesetup_postdata
then use standard APIs? If this was done withWP_Query
and a standard post loop that step would be unnecessary too. I also see you've setsuppress_filters
to true, despite this disabling all forms of caching and incurring a performance cost, and I also see you have lots of parameters with the value''
, what's the reasoning behind this?