basically, I'm trying to optimize this query we have more than 100K blogs and I need to make it faster now it takes Load time (07.46 s).
<?php
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
$query = new WP_Query(array(
'date_query' => array(
array(
'year' => date( 'Y' )-2,
'compare' => '>=',
),
),
'posts_per_page' => 10,
'paged' => $paged
));
while ($query->have_posts()): $query->the_post(); ?>
<article id="post-<?php the_id() ?>" class="card bg-light text-dark card-blog">
<?php the_post_thumbnail('large',['class'=>'card-img-top']); ?>
<div class="card-body">
<span class="card-meta">
<time datetime="<?php echo strtotime(the_time('F j, Y')); ?>"><?php echo the_time('F j, Y'); ?></time> | <?php the_category(', '); ?>
</span>
<h3 class="card-title"><?php the_title(); ?></h3>
<p class="lead"><?php echo substr(get_the_excerpt(),0,50).'...'; ?></p>
<div class="btn-container">
Learn More<?php gpi_sprite('arrow-right'); ?>
</div>
</div>
</article>
<?php endwhile; ?>
</div>
</section>
<?php
$pages = paginate_links( array(
'format' => '?page=%#%',
'current' => max( 1, get_query_var('page') ),
'total' => $query->max_num_pages,
'type'=>'array',
) );
?>
the above query grabbing all blog posts 10 posts per page but I show only the latest 2 years and this takes much expected than I imagine I appreciate any help
you can use these two keys before and after rather than Compare
'date_query' => array(
'after' => '2021-06-01',
'before' => '2023-06-01',
),
The wp_posts table has an index on (post_type, post_status, post_date,ID). If you only query by date you can't exploit that index, and your query may have to look at the entire table (which is large in your case).
You might try querying for only published posts. What you call blogs are called posts in WordPress parlance. That may allow the MySQL database to exploit the index. You could try this query (not debugged):
$query = new WP_Query(array(
'post_type' => 'post',
'post_status' => 'publish',
'date_query' => array(
array(
'year' => date( 'Y' )-2,
'compare' => '>=',
),
),
'posts_per_page' => 10,
'paged' => $paged
));
It will be hard to help you further until you use the Query Monitor plugin to investigate the slow query.
Related
I have two loops in my archive-inzerat.php, first is custom loop, second is default loop:
<div class="container">
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
$featured_query = new WP_Query( $featured_args );
if ( $featured_query->have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( $featured_query->have_posts() ) : $featured_query->the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
wp_reset_postdata();
?>
</div>
</div>
<?php
endif;
?>
<?php
if ( have_posts() ) : ?>
<div class="col-md-12">
<div class="row">
<?php
while ( have_posts() ) : the_post();
echo '<div class="col-md-3">';
get_template_part( 'content', 'inzerat' );
echo '</div>';
endwhile;
?>
</div>
<div class="clearfix"></div>
<div class="utf-pagination-container margin-top-20">
<nav class="pagination">
<?php
global $wp_query;
$total_pages = $wp_query->max_num_pages;
if ( $total_pages > 1 ) {
$current_page = max( 1, get_query_var( 'paged' ) );
echo paginate_links( array(
'base' => get_pagenum_link( 1 ) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => '<i class="fa fa-angle-left"></i>',
'next_text' => '<i class="fa fa-angle-right"></i>',
'type' => 'list',
'end_size' => 3,
'mid_size' => 3,
) );
}
wp_reset_postdata();
?>
<script>
jQuery('ul.page-numbers').addClass('pagination-custom');
//jQuery('ul.page-numbers li').addClass('page-item');
//jQuery('ul.page-numbers li a, ul.page-numbers li span').addClass('page-link');
jQuery('ul.page-numbers li span.current').parent().addClass('active');
</script>
</nav>
</div>
</div>
<?php
endif;
?>
</div>
</div>
In first loop I want to display only custom posts by custom field is_featured which is type boolean. In second loop I want to exclude these posts and display others to prevent duplications. Unfortunately, this code I use removes everything from the main loop:
function my_posts_filter( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'inzerat' ) ) {
$query->set( 'meta_query', array(
array(
'key' => 'is_featured',
'value' => '1',
'compare' => 'NOT IN'
)
));
}
}
add_action( 'pre_get_posts', 'my_posts_filter' );
What's wrong here?
I want also ask, will my pagination work and not break when adding custom loop?
Thanks for helping.
As per my opinion try this logic,
Define a blank array at the beginning of your file.
$firstloop_ids = array();
When your first loop is performed, you just need to add those IDs into that blank array. ( you can use array_push for that, reference: https://www.w3schools.com/php/func_array_push.asp )
For the second loop, you can exclude those ids using that array by this parameter.
'post__not_in' => $firstloop_ids,
This should work for you!
Try with post__not_in argument:
$featured_args = array(
'post_type' => 'inzerat',
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'publish_date',
'order' => 'DESC',
'post__not_in' => array(278),
'meta_query' => array(
array(
'key' => 'is_featured',
'value' => '1',
)
)
);
It will be definitely exclude specific posts.
I coded a wordpress-site and since some time the pagination worked fine. Then it stopped working.
I tried different plugins, but no one works. I want to put the pagination in a special place within a self-coded loop. Therefore the plugin of bestwebsoft seemed to be the best, since it offers the option of displaying the pagination by the use of a bit of PHP. But also that does not work. The support of bestwebsoft is not helpful, they only repeat what I can read in the documentation.
My questions:
1) Is there eventually a problem in my loop, that stops the pagination from displaying?
2) Is it a problem that I use "Masonry" for showing the excerpts and order the excerpts in a horicontal manner? Maybe that interferes with the pagination?
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
if ( function_exists( 'pgntn_display_pagination' ) ) {
pgntn_display_pagination( 'posts' );
}
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->
I want to have some kind of pagination. I would prefer a self-coded one, but could not do it. Therefore I also would accept a plugin-solution. but nothing works! Most of all i would like to know where the problem is located.
I don't know what is the code in pgntn_display_pagination function but I have modified your code with below one which will work for you.
Kindly check below code.
<div class="grid" data-masonry='{ "itemSelector": ".grid-item",
"columnWidth": 285, "gutter": 20 }'>
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; // get the current page variable and set it
$args = array(
'post_type' => array('post',
'os_buch_review',
'os_review',
'os_classic_review',
'os_versus',
),
'post_status' => 'publish',
'nopaging' => false,
'posts_per_page' => '20',
'order' => 'DESC',
'orderby' => 'date',
'cat' => '-5738,-1705, -5933',
'paged' => $paged // use $paged variable here
);
$tk_startteaser_querie = new WP_Query( $args );
if( $tk_startteaser_querie->have_posts() ) :
?>
<?php
while( $tk_startteaser_querie->have_posts() ) :
$tk_startteaser_querie->the_post();
?>
<div class="grid-item">
<a class="linkclass" href="<?php the_permalink(); ?>"></a>
<h3 class="entry-title"><?php the_title(); ?></h3>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('medium');
}
?>
<?php the_excerpt(); ?>
</div><!-- grid-item -->
<?php
endwhile;
// Below is full code of pagination
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Next', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Previous', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
wp_reset_postdata();
?>
<?php
else :
esc_html_e( 'Derzeit keine Beiträge!', 'text-domain' );
endif;
?>
</div><!--grid-->
I'm trying to make pagination for result of two post types which is one custom post type question and the normal post
first of all when I show all result without pagination using 'posts_per_page' => -1; the function works fine
but the problem happen when I try to make pagination, as you can see pagination function works normally except the some last pages of the pagination (I guess which contains question post_type even recent post type of questions appears normally )
here you are my full code
<?php
$my_query = new WP_Query(
array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
)
);
?>
How can I make pagination work normally and navigate to all pagination links ?
Replace your code this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => 0
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => $offset
);
}
$loop = new WP_Query($args);
?>
<?php
if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
install this free plugin https://wordpress.org/plugins/wp-pagenavi/
then refer below code
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'post',
'cat' => '53',
'paged' => $paged,
'posts_per_page' => '10',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part('parts/bl`enter code here`ogloop');
}
if(function_exists('wp_pagenavi'))
{
wp_pagenavi( array( 'query' => $query ) );
}
wp_reset_postdata();
} else {
echo 'no posts here';
}
This will show paging and work like a charm for you.
I guess that I figure it out
the solution is to edit the WP_Query args to be a custom number which is not same as site posts_per_page
'posts_per_page' => 11, // 13, or 20 ...
instead of
'posts_per_page' => get_option('to_count_portfolio'),
Why ? I've no idea, maybe the are some kind of conflict
someone can try it and see if that solution works with him/her same as me
i am using below code that seems to work fine on my local server but when i use it live server , it just displays first page content on every page .
content does not changes on moving to next page while , pagination index show currnet page index though.
below is my code :
<?php
/*
Template Name: My News
*/
get_header();
// First, let's see if we have the data in the cache already
$the_query = wp_cache_get('m-in-news'); // the cache key is a unique identifier for this data
if ($the_query == false) {
// Looks like the cache didn't have our data
// Let's generate the query
$args = array(
'posts_per_page' => 10,
'meta_query' => array(
array(
'key' => 'pdf_cin_file',
'compare' => '!=',
'value' => '',
),
),
'orderby' => 'post_date',
'paged' => $paged,
'order' => 'DESC',
'post_type' => 'minnews',
'post_status' => 'publish',
'cache_results' => true
);
$the_query = new WP_Query($args);
// Now, let's save the data to the cache
// In this case, we're telling the cache to expire the data after 300 seconds
wp_cache_set('m-in-news', $the_query, '', 300); // the third parameter is $group, which can be useful if you're looking to group related cached values together
}
$max_page = $the_query->max_num_pages;
$big = 999999999;
?>
<div class="master">
<?php get_sidebar('presslistpages'); ?>
<div class="rightSection">
<h1><?php echo esc_html(get_the_title()); ?></h1>
<div class="mNews">
<div class="data_gird_content">
<ul>
<?php
// Once we're here, the $query var will be set either from the cache or from manually generating the WP_Query object
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$pdflink = get_post_meta(get_the_ID(), "pdf_cin_file");
$pdflink = isset($pdflink[0]) ? $pdflink[0] : '';
?>
<li><div class="icon"><i class="fa fa-angle-double-right"></i></div> <div class="details"><h2><?php echo esc_html(get_the_title()); ?></h2></div></li>
<?php
}
}
wp_reset_postdata();
?>
</div>
<div class="paginationWrapper"><?php
echo wp_kses(paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $max_page
)), array('a' => array_merge(cci_allowed_a_tag_attr(), array(
'href' => array()
)),));
?></div>
</div>
</div>
</div>
<?php
get_footer();
?>
Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>