issue with using wp_cache_get on server - wordpress

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();
?>

Related

Wordpress - two loops in CPT archive page, exclude posts from main query

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.

WP_Query - posts_per_page parameter not working?

So the setup is working fine, however I tried to limit the number of titles shown but nothing is working. What am I missing? So it should be limited to 10 or 5 titles.
I tried everything I could think of but for some reason it is not taking the limit in account.
<?php
$meta_query = array();
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 1,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
);
$meta_query[] = array(
'key' => GOLO_METABOX_PREFIX. 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
);
$args_booking['meta_query'] = array(
'relation' => 'AND',
$meta_query
);
//$data_booking = new WP_Query($args_booking);
//$total_post = $data_booking->found_posts;
if( count($results) > 0 ){//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r):?>
<?php
$lang = $r->lang!='nl'?'/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li><?php echo get_city($r->city_id)->name;?></li>
<?php endforeach;?>
</ul>
<?php
}else{
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}
?>
There are a couple of filters that run before WP_Query actually executes the desired SQL command, which means you posts_per_page setting can actually get overridden before your results are returned.
Unfortunately this can be done in both the theme you may be using, and in any plugins that maybe active. What I would do in this situation, is first check any of my other code to see if I am modifying the pre_get_posts or the post_limits filter.
I'd then check the theme settings to see if it has a setting for this, and last resort would be to disable plugins one by one to see if any of those are filtering the query.
Lastly don't discount cache being an issue. Some hosts can force caching of pages, even when you are logged in to WordPress.
I corrected this like this.
So you can take a pattern.
Let me know if it does not work or does
I used posts_per_page to set the number of posts per page (number of titles per page) Or you can use numberposts.
numberposts is used to search and display only 20 posts and posts_per_page is used to display 20 posts per page.
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 20, // for set limit.
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
'meta_query' => array(
array(
'key' => GOLO_METABOX_PREFIX . 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
)
)
);
$query = new WP_Query($args_booking);
$results = $query->get_posts();
if (count($results) > 0) {//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r): ?>
<?php
$lang = $r->lang != 'nl' ? '/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li>
<a href="<?php echo $url; ?>" class="place-view"
data-id="<?php echo $r->ID; ?>">
<?php echo get_city($r->city_id)->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
} else {
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}

How to optimize posts query in wordpress

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.

Wordpress Custom post page with wp_pagenavi doesn't work

I have custom page for posts which has to show up 9 posts per page and I used wp_pagenavi plugin but it doesn't work. Please help!
<div class="page-content__wrapper">
<?php
$post_category = get_field('page_category');
$posts = get_posts( array(
'numberposts' => 9,
'category_name' => $post_category,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
//...posts
<?php
}
wp_pagenavi();
wp_reset_postdata();
?>
</div>
I used WP_Query() to solve this problem (without wp_pagenavy plugin :)
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup
$post_category = get_field('page_category'); // getting category name
$posts= new WP_Query(array(
'post_type'=> 'post',
'posts_per_page' => 9,
'paged' => $paged, // don't forget to give this argument
'category_name' => $post_category,
));
if($posts->have_posts()) :
while($posts->have_posts()) : $posts->the_post(); ?>
// All posts will be here!
<?php endwhile; ?>
<div class="mt-30 text-center">
<?php
$GLOBALS['wp_query'] = $posts;
the_posts_pagination(
array(
'mid_size' => '2',
'prev_text' => '<i class="fa fa-hand-o-left"></i> <',
'next_text' => '> <i class="fa fa-hand-o-right"></i>',
'screen_reader_text' => ' '));
?>
</div>
<?php else :?>
<h3><?php _e('404 Error: Not Found'); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Wordpress: Iterative Parent Page if Custom Field Contains Page ID

I am trying to display a list based on the page. The list is built based on data in an ACF custom field, which is in a serialized array.
The list is for employees in a department. I am setting the parent page in Wordpress but want the employees to be listed on all child page. Employees may also need to be listed in multiple departments, so the ACF field is stored as a serialized array.
I have been able to get this to work if the current page is in the array, but am having problems when the data is for a parent, grandparent, etc. page. How can I iterate through this to find the first parent page that matches the criteria?
Relevant code (additional content is displayed, some included in case it is helpful in your answer):
$postid = get_the_ID();
$staffList = get_posts(array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
)
)
));
if(( $staffList ) != '') {
foreach ( $staffList as $staff ) : setup_postdata( $people );
$pid = $staff->ID;
$featured_img_url = get_the_post_thumbnail_url($pid, 'full');
$highres = get_field('high_res', $pid);
$normal = get_field('normal', $pid);
$biofile = get_field('bio_file', $pid);
$scontent = $staff->post_content;
$contact = apply_filters('the_content',$scontent);
?>
<div class="profile">
<div class="text-center">
<img src="<?php echo $featured_img_url; ?>" width="130"
alt="<?php echo $staff->post_title; ?>">
</div>
<div class="profile__name"><?php echo $staff->post_title; ?></div>
<?php echo $contact; ?>
<?php if ( $biofile != "" ) { ?>
<span class="icon-file"></span> <?php echo $people->post_title; ?> Bio
<?php } ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
<?php } endif; ?>
I'm thinking the query needs to be placed in a function, but having a problem getting it to run correctly and then iterate through parent posts until it finds a results. The concept is:
if no employees for current $pageid, then get $parent_pageid and run query. if no employees found, find grandparent_pageid and run query, etc., until either employees are found or there are no more parent pages.
I've found this: Recursive function to get the parent caegory description that seems like a good way to iterate, just having a problem modifying for my needs.
Thanks for your help!
This function will loop through parent posts until it returns results:
function get_staff_posts($post) {
$staffList = new WP_Query( array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
),
));
$parent = $post->post_parent;
if( !$staffList->have_posts() && $parent != 0 )
get_staff_posts($parent);
return $staffList;
}
The function will return the query, so you could use it in your theme like this:
$staffList = get_staff_posts($post);
if( $staffList->have_posts() ):
while( $staffList->have_posts() ): $staffList->the_post();
// output
endwhile; wp_reset_postdata();
endif;

Resources