How to paginate wordpress using get_posts - php

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

Related

How to create a query for displaying the lessons from the current category(course) LearnDash customization?

I try to customize the course content list forming by the LearnDash plugin. Now on the lesson page in the sidebar displays just a boring list of the lessons included in the course (the current shortcode formed it). But I need to display the list of the lessons with thumbnails and other meta information from the lessons. For this purpose, I try to use this chunk of code:
<?php
$args = array(
'cat' => 153,
'post_type' => 'sfwd-courses',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
And I always see the same answer: "No Posts Sorry". Please help to sort out this issue.
Please try below code which helps you to display lessons fo specific category.
<?php
$args = array(
'post_type' => 'sfwd-lessons',
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'ld_lesson_category', //double check your taxonomy name in you db
'field' => 'id',
'terms' => 26,
),
),
);
$q = new WP_Query($args); ?>
<div class="timeline"> <?php
if ($q->have_posts()) : while ($q->have_posts()) : $q->the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>

Posts per page not limiting posts in query

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

pagination doesn't work on a single page of WordPress

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.

Get posts from each category once inside a section tag with category as id

$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories = get_categories($args);
// print_r($categories);
foreach ($categories as $cat) { ?>
<section id="<?php echo $cat->slug ?>-section"><?php
$args2 = array(
'post_type' => 'post',
'category_name' => $cat->slug
);
$query = new WP_Query( $args2 );
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();?>
<h2 class="<?php echo $cat->slug ?>-heading">
<?php echo get_the_title() ?>
</h2>
<div class="post-date">
<?php the_time('d. m. Y') ?>
</div>
<div class="post-content">
<?php the_content(); ?>
</div><?php
endwhile;
endif;
wp_reset_postdata(); ?>
</section> <?php
}
gets me all posts from each category inside a section tag with post category as id.
However this also gets me the same result without using new Wp_Query.
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$categories = get_categories($args);
// print_r($categories);
foreach ($categories as $cat) { ?>
<section id="<?php echo $cat->slug ?>-section">
<?php
// Get posts from each category
$args2= array("orderby"=>'name', "category" => $cat->cat_ID);
$posts_in_category = get_posts($args2);
foreach( $posts_in_category as $current_post ) { ?>
<h2 class="<?php echo $cat->slug ?>-heading">
<?php echo $current_post->post_title; ?>
</h2>
<div class="post-date">
<?php the_time('d. m. Y') ?>
<?php echo $current_post->post_date; ?>
<!-- <?php // echo $current_post->the_time('d. m. Y') ?> -->
</div>
<div class="post-content">
<?php the_content(); ?>
<?php echo $current_post->post_content; ?>
</div> <?php
}
?>
</section> <?php
}
In the second approach I cannot set the date as I want to with echo $current_post->the_time('d. m. Y') but would have to format post_date what is kind of tricky.
Not being familiar with not working with new WP_Query but with $args2= array("orderby"=>'name', "category" => $cat->cat_ID); and using get_posts does not feel right to me.
Both approaches query the database for each category, one with new WP_Query and the other with get_posts.
What is the best practise and performant way to query the database so that all posts inside each category are shown inside a section tag with category as id?

How to show WordPress Post and page in category

I’m trying to create many WordPres post and page. I include the post and page to various category. In each category I add post and page both. In this circumstances I need to show post and page under the particular category. And I want to sort the post and page ascending or descending under the Category. I need the PHP Coding this purpose. Please Give me assistance. I have created category.php by the code bellow.
<div class="cate-top ">
<h1 class="cat-page-title"><?php printf( __( ' Your are Browsing: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<?php if ( category_description() ) : // Show an optional category description ?>
<div class="archive-meta"><?php echo category_description(); ?></div>
<?php endif; ?>
<?php while(have_posts()): the_post();?>
</div>
<div class="category-page">
<div class="cate-inn ">
<h2> <?php the_title();?></h2>
<div class="cat-image fix">
<?php the_post_thumbnail();?>
</div>
<div class="cat-read-more fix">
<?php read_more(0);?>Read More
</div>
</div>
<?php endwhile;?>
You can use get_posts or WP_Query to get the page and post with your desired category, For example
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
You can just change your category name in args,
If you are using custom taxonomy instead default category, you may use following code
$custom_terms = get_terms('custom_taxonomy');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'';
endwhile;
}
}
For more help you can VISIT, for get_post you may VISIT

Categories