I am pretty new to wordpress, and wondering if someone could shed some light on this code.
I am trying to list all sub pages on their parent page, here is the code with some html stripped out:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
The code works, and correct sub pages are displayed - but not all of them. The 7 oldest posts are showing, but none of the newest pages that I created this week. I have checked and double checked that all new and old pages are the same in every way - same template, same parent page, same creator, same order number, and all published. Anyone have an idea of what I could be doing wrong?
Try below code:
$args = array('child_of' => $post->ID,'sort_order' => 'desc',
'sort_column' => 'ID',
);
I think you need additional argument as well to get result you are looking for.
$args = array(
'child_of' => $post->ID,
'parent ' => $post->ID,
'hierarchical' => 0,
'sort_column' => 'menu_order',
'sort_order' => 'asc'
);
$mypages = get_pages( $args );
You can check wordpress Doc for its argument and return output.
NOTE If you want to sort pages by date then you can change 'sort_column' => 'menu_order', with 'sort_column' => 'post_date',.
There is other method as well to achieve the same and I prefer below way.
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order'
);
$mypages = new WP_Query( $args );
if ( $mypages->have_posts() ) : ?>
<?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?>
<p style="color: white; text-transform: uppercase;"><?php the_title(); ?></p>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
You can also use wp_list_pages to render direct HTML.
Try using parent instead of child_of and setting hierarchical to false. According to the docs it appears the default hierarchical value of true can can affect the results.
<?php
$mypages = get_pages( array( 'parent' => $post->ID, 'hierarchical' => 0 ) );
?>
Reference: https://codex.wordpress.org/Function_Reference/get_pages
I have fixed the issue, but I actually have no idea why this works. Any comments about why this did the trick would be awesome.
From my original code, I removed the "if ( ! $content )" section, and they all show up - even though all pages have the same amount of content.
So, in the end, my code reads:
<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );
foreach( $mypages as $page ) {
$content = $page->post_content;
$content = apply_filters( 'the_content', $content );
?>
<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>
<?php
}
?>
I never had to change the get_pages function parameters at all.
Related
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
}
Hello—so what I'm attempting is to get 2 posts from 2 taxonomy terms, I've got that part done, where I'm borking up is trying to display the term name above the posts. I've done this before using a foreach loop but no matter what I change, I keep getting the error that I've supplied invalid arguments for the loop. After a lot of googling, I'm a bit lost and wondering if you folks have some guidance for me?
$current_post_id = get_posts( array(
'post_type' => 'issue',
'posts_per_page' => 1,
'fields' => 'ids',
) );
$terms = get_the_terms( $current_post_id, 'department' );
foreach ( $terms as $term ) {
$args = array(
'connected_type' => 'posts_to_issues',
'connected_direction' => 'to',
'connected_items' => $current_post_id,
'post_type' => 'post',
'posts_per_page' => 2,
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'slug',
'terms' => array( 'washington-watch', 'equipment-spotlight'),
)
),
);
$sidebar_query = new WP_Query( $args );
while ( $sidebar_query->have_posts() ) : $sidebar_query->the_post(); ?>
<?php echo '<h2>' . $term->name . '</h2>' ?>
<figure>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'sidebar-thumb-med' ); ?>
</a>
</figure>
<h4><?php the_title(); ?></h4>
<?php the_excerpt();
endwhile;
}
// Restore global post data
wp_reset_query();
$current_post_id is an array with one item (because passing in a return fields argument returns an array). You need to refer to that one item in the following cases:
$terms = get_the_terms( $current_post_id[0], 'department' );
...and...
'connected_items' => $current_post_id[0],
I am trying to get the two most recent post but however i want to exclude a specific post by its id[365]. Can anyone help me with it? here's my code.
$args = array( 'numberposts' => '2' , 'post__not_in' => array( '365' ) );
$recent_posts = wp_get_recent_posts( $args );
<?php foreach($recent_posts as $post):?>
<p><?php echo $post['post_title'];?></p>
<?php endforeach;?>
Just need to replace your code by below mentioned code, you will get your desired result :
<?php $my_args = array('post_type' => 'post' , 'numberposts' => '2' , 'exclude' => '365' );
$my_recent_posts = wp_get_recent_posts( $my_args );?>
<?php foreach($my_recent_posts as $my_post):?>
<p><?php echo $my_post['post_title'];?></p>
<?php endforeach;?>
You should read the docs for that function again. You have an exclude parameter available:
$args = array( 'numberposts' => '2' , 'exclude' => 365 );
wp_get_recent_posts Does not support post__not_in argument you will need to use exclude
Reference link for wp_get_recent_posts
<?php
$args = array( 'numberposts' => '2' , 'exclude' => '365' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post){
?>
<a href="<?php echo $post['guid']; ?>">
<p><?php echo $post['post_title']; ?></p>
</a>
<?php } ?>
how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.
I have display all page content in current page but not current page content to be display so bellow code give me all page content but how can i filter it ?
Thanks.
$pages = get_pages();
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
echo $content;
}
Try this code: it will exclude the current page by taking its ID.
<?php
$args = array(
'post_type' => 'page',
'numberposts' => -1,
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'post_status' => 'publish',
'exclude' => get_the_ID()
);
//$allpages = get_pages($args ); ?>
<?php wp_list_pages( $args ); ?>