I have a query that spits out posts that aren't featured but for some reason the posts_per_page limiter isn't working...
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
Thanks for any insight on this issue.
Yeah, use 'nopaging' => true and 'ignore_sticky_posts'=>true
<?php $featured_posts = get_posts( [
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'nopaging' => true,
'ignore_sticky_posts'=>true,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'fields' => 'ids',
] );
query_posts( array( 'post__not_in' => $featured_posts ) );
while ( have_posts() ) : the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile;
wp_reset_query();
?>
The problem you are facing here is that you are getting posts object in $featured_posts and thus calling query_posts is really unnecessary. From your code structure, I am assuming that you are trying to access the main query but with modified parameters. A better approach to accomplish your goal is as follows.
$args = array(
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!=',
'fields' => 'ids',
);
$featured_posts = new WP_Query( $args );
while ( $featured_posts->have_posts() ) : $featured_posts->the_post();?>
<div class="news-item-block col-md-4" role="article">
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('news-grid-image'); ?>
</a>
<span class="news-item-date"><?php the_date( 'M d, Y' ); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php
endwhile;
wp_reset_query();
On a side note, I'd advise against using query_posts as it directly modifies the main query. But hopefully the above code should help you achieve desired results.
Got it. The trick was to target the while function e.g.:
<?php $i = 1; while (have_posts() && $i < 4) : the_post();?>
<?php the_post_thumbnail(); ?>
<?php the_date( 'M d, Y' ); ?>
<?php the_title(); ?>
<?php $i++; endwhile;?>
Related
Good afternoon, I'm extending my template where materials are displayed through additional groups of fields, and I display them on the site using the form below, the main materials and posts are fine, but I need to implement a page with a blog and display news there, I would like to make pagination ( page switching) like page 1-2-3 but the main function wp_pagenavi(); does not work, read many forums but did not find a solution
<?php
$posts = get_posts( array(
'numberposts' => -1,
'category' => 0,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' =>'',
'post_type' => 'post',
'suppress_filters' => true,
) );
foreach( $posts as $post ){
setup_postdata($post);
?>
<div class="cpt-alm-item cpt-alm-col-12 ">
<div class="post_min" style="background-image:url(<?php echo get_the_post_thumbnail_url(); ?>);">
<div class="post_date"><?php echo date( 'F j, Y' ); ?></div>
</div>
<a href="<?php the_permalink() ?>" class="post_zag">
<p><?php the_title(); ?></p></a>
</div>
<?php
}
wp_reset_postdata();
?>
I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive
Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.
i want to get archives of multipal custom post types can u help me guys here is my code which is not working
<?php $args = array(
'type' => 'yearly',
'limit' => '',
'format' => 'custom ',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => array('news','update')
);
wp_get_archives( $args );
Try This:
<?php
$args = array(
'post_type' => array('news','update'),
'post_status' => 'archive',
'type' => 'monthly',
'echo' => 0,
'order' => 'DESC'
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php post_thumbnail( 'thumbnail' );?>
</a>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php the_excerpt(); ?>
</article>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
I have a problem showing numbered pagination on only one of my Wordpress pages.
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<?php endif; ?>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php the_posts_pagination(array(
'prev_text' => '«',
'next_text' => '»',
)); ?>
</div>
</div>
</div>
</div>
</div>
The query returns about 70 products, so I would expect it to show at least 7 numbers. However, it does not show any pagination.
If you give your full code it will be more easier to find out your problem.
May be you use wp_reset_postdata(); before the_posts_pagination();
Please check this.
try this code it surely help you
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => array (
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'meta_query' => array (
'key' => 'prijsknaller',
'value' => '1' ,
'compare' => '==',
//'type' => 'date',
),
);
$wp_query = new WP_Query( $args );
Replace your code with this:
<?php
/**
* Template Name: Sale template
*/
?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => 0,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array(
'houten_vloeren', 'keramische_tegels', 'natuursteen_vloeren', 'tegels', 'laminaat', 'pvc_vloeren', 'tafels_stoelen',
),
'posts_per_page' => 5,
'post_status' => 'publish',
'paged' => $paged,
'offset' => $offset,
'meta_query' => array(
'key' => 'prijsknaller',
'value' => '1',
'compare' => '==',
//'type' => 'date',
),
);
}
$query = new WP_Query($args);
?>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="product-overview">
<?php if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post(); ?>
<div class="col-md-3">
<div class="featured-block">
<div class="featured-block-image">
<a href="<?php the_permalink(); ?>"><img src="<?php the_field('productafbeelding'); ?>"
alt="<?php the_title(); ?>"></a>
</div>
<div class="featured-block-info">
<h4><?php the_title(); ?></h4>
<?php if (get_field('prijs_oud')) : ?>
<span class="oud"><?php the_field('prijs_oud'); ?></span>
<span class="nieuw"><?php the_field('prijs_nieuw'); ?></span> p/m<sub>2</sub>
</div>
</div>
</div>
<?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>
</div>
</div>
</div>
</div>
</div>