Wordpress displaying only children posts - wordpress

im trying to display only children posts, i don't need pagination. In theory this code should query all posts and in second query get posts that have parent. But in reality it displaying all posts with parent.
<?php
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args1 = [
'post_type' => 'drama',
'post_parent' => 0, // Only return top level pages
'nopaging' => true, // Alias of posts_per_page => -1, Get all top level pages
'fields' => 'ids' // Only get pages ID's for performance
];
$exclude_parents = get_posts( $args1 );
// Now we can run our query as normal
$args = [
'post_type' => 'drama',
'post__not_in' => $excluse_parents, // Exclude parent pages
'paged' => $paged,
];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php $parent_p = wp_get_post_parent_id( $post_ID ); ?>
<?php if ( 0 == 0 ) { ?>
<div class="col-md-12"><span><?php the_title(); ?></span></div>
<?php } ?>
<?php endwhile; wp_reset_postdata(); ?>
<div class="clearfix"></div>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>

If you are looking for an algo to get those posts which have no child post then the algo should be
Step 1 : get all post id in an array(post_id_arr).
Step 2: get all post's parent id in another array(parent_id_arr);
Step 3: run a loop for parent_id_arr and remove the parent ids from post_id_arr.
Step 4: Now the post_id_arr will have only child post ids ,

Related

Cannot get pagination working in Wordpress Page template, already tried and failed a lot

I'm trying to get pagination working in a page template that I am making. I have already been through multiple threads on this website and I've also been searching through the Wordpress.org documentation (and the comments). Every time I reload my page, there is nothing where the pagination should be.
I've tried both get_posts() and WP_query(). WP_query is the one I believe is needed so that's the one I'll be giving you examples from.
Things I've already tried:
Using $paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
Also tried that last one with 'paged' instead of 'page'.
Using this in the arguments: 'posts_per_page' =>6, 'paged' => $paged,
And using this in the arguments 'posts_per_page=6&paged=' . $paged, 'paged' => $paged,
Tried resetting the query of my loops with wp_reset_query();
There are some other things I've tried too but the above are the ones I see recommended most often.
This is my full code in its current state (I've removed some things to help you read it).
I have tried to make the first loop with get_posts() instead and that didn't make a difference. I've also tried positioning commands like the wp_reset_query() in several different places.
The eventual goal is to have several of these loops on the same page, all except the first with pagination under them (I will be using ajax to allow users to cycle through the different content).
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = '
category-name1,category-name2
';
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = ( get_query_var( 'page' ) ) ? absint( get_query_var( 'page' ) ) : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php
endwhile; endif;
pagination_bar();
?>
</div>
Try this
<?php /* Template Name: Home */ ?>
<div>
<?php
/*
First Loop - This one will stay the same on every page. Uses the same categories as the second loop.
*/
$categories = array('category-name1' , 'category-name2');
$query = new WP_Query( array( 'posts_per_page' =>4, 'category_name' => $categories ) );
?>
<div>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile; wp_reset_query(); endif; ?>
</div>
</div>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'posts_per_page' =>6, 'paged' => $paged, 'category_name' => $categories, 'offset' => '4' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div>
<?php get_template_part( 'entry-boxes-1' ); ?>
</div>
<?php endwhile;
$total_pages = $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' => false,
'next_text' => false,
));
}
wp_reset_postdata();
endif;
?>
</div>

display wp custom posts by catgeory

Added this code to display wp custom post by category, but unable to get pagination to work when added posts_per_page="5"
<?php query_posts('post_type=encounters_news'); while (have_posts()) : the_post(); ?>
<?php $terms = get_the_terms($post->ID, 'encounters_news_categories');
foreach($terms as $item):
if($item->slug == "chapter-news"):?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php //comments_template( '', true ); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; wp_reset_query(); ?>
<?php encounters_content_nav( 'post-nav' ); ?>
Rewrote and change to this code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$post_type = 'encounters_news';
$tax = 'chapter-news';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$query_args = array(
'post_type' => $post_type,
'$tax' => $tax_term->slug,
'showposts' => 5,
'paged' => $paged
); wp_reset_query();
}}
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="navigation">
<div class="alignleft"><?php echo get_previous_posts_link( '« Previous' ); // display newer posts link ?></div>
<div class="alignright"><?php echo get_next_posts_link( 'More »', $the_query->max_num_pages ); // display older posts link ?></div>
This code creates the pagination as desired, but displays all the Encounters News Categories instead of just the Chapter News category.
Suggestions will be greatly appreciated.
Resolved
<?php
$query = new WP_Query( array(
'post_type' => '', // name of post type.
'tax_query' => array(
array(
'taxonomy' => '', // taxonomy name
'field' => '', // term_id, slug or name
'terms' => , // term id, term slug or term name
)
),'showposts' => 5,
'paged'=>$paged
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();?>

Wordpress - page as category

I researched several tutorials and nothing ...
can someone tell me how do I print only the category "news" in the article page?
Theme Onepress*
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'list' );
?>
<?php endwhile; ?>
<?php the_posts_navigation(); ?>
<?php else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
</main><!-- #main -->
Maybe the easiest way is to create your own query:
// WP_Query arguments
$args = array (
'post_type' => array( 'news' ),
'post_status' => array( 'publish' ),
'posts_per_page' => '-1',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
This gives you all the query results that are: published and of the 'news' custom post type.
// WP_Query arguments
$args = array (
'post_status' => array( 'publish' ),
'category_name' => 'news',
'posts_per_page' => '-1',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
The second one gives you all the query results, that are published and in the category 'news'.

Show sticky posts at the top, then display other posts

I have this query:
<?php $wp_query = array(
'post__not_in' => array(4269),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 5,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
?>
It's currently showing upcoming posts in order... how can I show the sticky posts at the top, then show the other posts below it?
E.g If two posts have been marked as sticky then they would show at the top, then the other 3 posts will just be the upcoming posts.
I had a similar problem a while back and devised the following solution. This will show you how to output a maximum of five posts, with the sticky ones at the top. You will have to make your own adjustments to the arguments array, but this should point you in the right direction.
It's a matter of determining how many sticky posts were in fact displayed, subtracting that number from 5, then displaying the balance of non-sticky posts.
<?php
function define_loop_args($present_cat, $sticky_toggle = 0 ) {
/*the total number of posts to display*/
$this->maxNum = 5;
$loop_args = array(
'cat' => $present_cat,
);
if ( $sticky_toggle == TRUE ) {
$loop_args['post__in'] = get_option( 'sticky_posts' );
$loop_args['posts_per_page'] = $this->maxNum;
} else {
$loop_args['post__not_in'] = get_option( 'sticky_posts' );
$loop_args['posts_per_page'] = ((int)($this->maxNum) - (int)($this->sticky_count));
}
$this->loop_args = $loop_args;
}
?>
<ul class="no-children">
<?php
/*
* STICKY
*output sticky posts first
*/
$this->define_loop_args( $catID, 1 );
/*
*count the number of sticky posts displayed, in order to calculate how many non-sticky posts to output next
*/
$sticky_count = 0;
// The Query
$the_query = new WP_Query( $this->loop_args );
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?>
<?php
$sticky_count++;
endwhile;
// End The Loop
// Reset Post Data
wp_reset_postdata();
$this->sticky_count = $sticky_count;
/*
* NON-STICKY
*output non-sticky posts next
*/
$this->define_loop_args( $catID );
$the_query = new WP_Query( $this->loop_args );
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?>
<?php
endwhile;
// End The Loop
// Reset Post Data
wp_reset_postdata();
?>
</ul>
you can get it ny multiple loop as follows
<?php
$sticky = get_option( 'sticky_posts' );
$args_ordinary = array(
'post__not_in' => array(4269,$sticky),
'post_type' => 'whatson',
'exclude' => '4269',
'posts_per_page' => 3,
'order' => 'ASC',
'orderby' => 'date',
'post_status' =>array('future','published'));
$args_sticky = array(
'posts_per_page' => -1,
'post__in' => $sticky,
'posts_per_page' => 2,
'post_type' => 'whatson'
);
query_posts($args_sticky);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
//sticky post
<?php endwhile; ?>
<?php endif; ?>
// Now Ordinary Posts
query_posts($args_ordinary);
if (have_posts()): ?>
<?php while (have_posts()) : the_post(); ?>
//ordinary post
<?php endwhile; ?>
<?php endif; ?>

Wordpress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page

Hey everyone. I'm not sure if what I'm experiencing is a result of a bug (due to the recent upgrade to 3.1.2) or poor coding. Ever since i upgraded to version 3.1.2 ive been experiencing a problem with two loops on my index page.
Here's what I've got for my index page
<?php
if ( ! is_paged() && is_front_page() ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
$sticky = get_option( 'sticky_posts' );
if ( isset( $sticky[0] ) ) {
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
'ignore_sticky_posts' => 1);
// Query
$featured_query = new WP_query( $args );
while ($featured_query->have_posts() ) :
$featured_query->the_post();
$featured[] = $post->ID;
get_template_part( 'content', 'featured' );
endwhile;
} // endif sticky
} // endif is_paged
?>
<?php
$sticky = get_option( 'sticky_posts' );
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $featured,
'post__not_in' => $sticky
);
query_posts($query_args);
if (have_posts() ) :
while (have_posts() ) :
the_post();
get_template_part( 'content', get_post_format() );
?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; else: ?>
<div class="box">
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
</div>
<?php endif; ?>
// Navigation comes over here
Say for example the first loop (sticky posts) - which IS NOT paged, yields 3 posts, and the second loop (all other posts) - which IS paged, yields 10 posts. The problem I'm experiencing is that when i move to the next page, the last 3 posts from the second loop on page 1 get repeated at the the top of page 2.
Note: The first loop is only on page 1, and doesn't get repeated on the second page, which is what i intended.
So this is what i tried, i removed the ( ! is_paged() && is_front_page ) condition along with the entire first loop, and the problem got resolved.
What am i doing wrong?
After your first loop, try adding
wp_reset_postdata();
I'm not sure if your trying to only have the first loop on the first page, but if you are, try something like
$paged = get_query_var('page');
if ($paged < 2) :
// Put whatever you want to only show up on the first page here
endif;
Thanks Chris,
I changed your suggestion (which didnt seem to work)
$paged = get_query_var('page');
if ($paged < 2) :
// Put whatever you want to only show up on the first page here
endif;
to
$paged = get_query_var('paged');
if ($paged < 1 ) {
// code goes here
}
It seems as if the first page isnt considered "paged".. "paged" only applies to pages beyond the first page.
this is the updated code for anyone who's interested. Hat tip to Chris. Thanks again.
$paged = get_query_var('paged');
if ($paged < 1 ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
$sticky = get_option( 'sticky_posts' );
if ( isset( $sticky[0] ) ) {
$args = array(
'posts_per_page' => 3,
'post__in' => $sticky,
'ignore_sticky_posts' => 1);
// Query
$featured_query = new WP_query( $args );
while ($featured_query->have_posts() ) :
$featured_query->the_post();
get_template_part( 'content', 'featured' );
endwhile;
wp_reset_postdata();
} // endif sticky
} // endif $paged
?>
<?php
$sticky = get_option( 'sticky_posts' );
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$query_args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $sticky
);
query_posts($query_args);
if (have_posts() ) :
while (have_posts() ) :
the_post();
get_template_part( 'content', get_post_format() );
?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; else: ?>
<div class="box">
<p>
<?php _e( 'Sorry, no posts matched your criteria.' ); ?>
</p>
</div>
<?php endif; ?>
an alternative to the previous example was one which i built from the ground up before Chris answered is
<?php if ( isset( $sticky[0] ) && ! is_paged() ) {
echo '<h6 class="sec1 title">FEATURE</h6>';
} ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_sticky() ) {
get_template_part( 'content', 'featured' );
} ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<?php
echo '<h6 class="sec1 title">LATEST ARTICLES</h6>';
global $sticky;
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'post__not_in' => $sticky
);
query_posts( $args );
while ( have_posts() ) :
the_post() ;
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<!--<?php trackback_rdf(); ?>-->
<?php endwhile; ?>
https://wordpress.stackexchange.com/questions/126814/wordpress-static-page-pagination/139594#139594
this post solved my problem for multiple loops with pagination.

Resources