wordpress and future posts - wordpress

I need to show future posts in all posts list on page.
my code
$args = array('posts_per_page' => 10,
'category' => $category->cat_ID );
?>
<?php $posts = get_posts($args); ?>
what arguments I can to write for showing all posts from category with future posts?
And it my archive page code, what should to show posts list from category
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php endif; ?>
this solution mast to show future and past posts too

You should specify the post_status to be publish and future in your $args, for example:
$args = array(
'posts_per_page' => 10,
'category' => $category->cat_ID,
'post_status' => array(
'publish',
'future'
)
);
To get the posts with these args, use (for example):
$posts = get_posts($args);
To display these posts, use (for example):
foreach( $posts as $p ) {
// display the post title
echo '<h2>' . apply_filters('the_title', $p->post_title) . '</h2>';
// display the post date
echo '<h4>' . date('F jS, Y', strtotime($p->post_date)) . '</h4>';
// display an excerpt of the post
echo wp_trim_excerpt($p->post_content);
}
An alternative is to use query_posts / WP_Query to get these posts. In this case, to display the posts, you would use a loop, similar to the one in your question. Such example can be seen here: http://codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop

Related

How to get taxonomy in a div

I want to add a taxonomy in my div with the different other fields, but I'm new to wordpress and O don't find how doing this, if someone can help me please ?
<?php/* Template Name: Archive Projets */ ?>
<?php get_header(); ?>
<?php
$posts = get_posts(array(
'posts_per_page' => 4,
'post_type' => 'projects'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post );
?>
<div>
<?php the_title(); ?>
<div><?php the_field('url')?></div>
<div><?php the_field('')?></div> /* HERE I WANT TAXONOMY */
</div>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Here's a code that will get the taxonomies of the current post and display them in a list of insert this code under <div><?php the_field('url')?></div>
// Get all terms
$terms = get_the_terms( $post->ID , array( 'Taxonomy_name') );
// Dispaly the taxonomies, if there one or more.
foreach ( $terms as $term ) {
echo '<div>' . $term->name . '</div></br>';
}
Make sure to change the 'Taxonomy_name' with your taxonomy name. let me know if I got you wrong or if you need any help with the code.
A quick google search brings up get_terms().
$terms = get_terms( array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
) );
foreach($terms as $current_term) {
//You can now loop through them and get the ones you want, display them all, or whatever else it is you want to do. Note: each $current_term is an object of type WP_Term (or error if there are no results).
}

Get values from most recent post

I have number values on my most recent post that are placed via advanced custom fields. I want to be able to pull the data from a post into another page. this can be easily done via an ID:
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
but what I cannot accomplish is having this pull from the most RECENT post. ACF support site make no mention of most recent.
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
// acf query in here. not included for brevity.
endif;
}
wp_reset_query();
?>
From the Codex on https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ (with slight modifications):
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 1, // Number of recent posts thumbnails to display
'post_status' => 'publish' // Show only the published posts
));
foreach($recent_posts as $post) : ?>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
<p class="custom-class"><?php echo $post['post_title'] ?></p>
</a>
<?php
$custom_field = get_field('custom_field_name', $post['ID']);//for ACF fields
?>
<?php endforeach; wp_reset_query(); ?>

Wordpress: Create a list of posts within a category

I thought this was going to be simple, but after hours of trying I still can't get it to work.
All I want to do is create a list of links to posts within a specific category and put it in the footer of my site.
Note: I do not want to use yet another plugin!
Try this:
<?php
// The Query
query_posts( array ( 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) );// -1 will display all posts, change to limit posts
// The Loop
while ( have_posts() ) : the_post();
echo '<li>', '<a href="<?php echo get_permalink( 268 ); ?>">';
the_title();
echo '</a>', '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
OK, so after getting Dungey140's solution to work this is how I did. This returns the correct URL for the posts too.
<?php
query_posts ( array ( 'post_type' => 'the-post-type', 'category_name' => 'The Category Name', 'posts_per_page' => -1 ) );
while ( have_posts() ) : the_post(); { ?>
<li><?php echo the_title(); ?></li>
<?php }
endwhile;
wp_reset_query();
?>

WordPress Get Terms and Posts Query is Breaking Other Posts

I have the following query, which is designed to show a list of terms in a taxonomy, and under each term, a list of posts assigned to that term.
This appears in a sidebar on the left hand side of my single.php posts page. In the main area of the page, the actual single post is meant to display.
However, instead of displaying the current single post, it is only displaying the TITLE for the most recent single post.
Here is my query:
$terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug);
$query = new WP_Query ($wpq);
echo "<li class=\"list-item\">".$term->name.""; ////
echo "<ul class=\"children\">";
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
echo "</ul></li>";
}
echo "</ul>";
I have tried adding reset query to the code, to no avail. I have identified that it is this specific section that is causing the problem:
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
What exactly am I doing wrong here? I have been trouble shooting this for 30-40 minutes now without any success.
Would appreciate an explanation of my error.
try to use
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
echo "<li>".the_title()."</li>";
<?php endwhile; endif; wp_reset_query(); ?>
in place of
$posts = $query->posts;
foreach ($posts as $post) {
echo "<li>".$post->post_title."</li>";
}
hope this will work fine for you
Your arguments for WP_Query aren't well formed.
Here's how it should be for taxonomy (snippet taken from the Codex ):
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'people',
'field' => 'slug',
'terms' => 'bob'
)
)
);
After you get your arguments right, you should loop over the results:
while( $query->have_posts() ):
$query->the_post();
// It is now that get_permalink() will work
endwhile;

wordpress - ordering multiples post types

i'm trying to order multiples post types in a page
my solution below is working but the posts are not showing by type after type exactly
<?php $args = array_merge( $wp_query->query,
array( 'post_type' =>array( 'editorial','video','portfolio' ),'posts_per_page=-1','orderby'=>'post_type') );
}
query_posts( $args );
$postType='';
if (have_posts()) : while (have_posts()) : the_post();
$PT = get_post_type( $post->ID );
if($postType != $PT){
$postType = $PT;
echo '<p class="clearfix"><h1>All posts of type : '.$postType.'</h1></p>';
}
?>
<?php
if($postType == 'editorial'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'video'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'portfolio'){
<?php echo $postType; ?>
}
?>
and it output like that:
All posts of type : editorial
editorial
editorial
editorial
All posts of type : video
video
video
All posts of type : editorial //problem
editorial
All posts of type : portfolio
portfolio
portfolio
portfolio
portfolio
thank you in advance
According to the Codex, post_type isn't one of the allowed values for the orderby parameter.
One way around your problem would be to use the posts_orderby filter, something like this:
function order_posts_by_type($original_orderby_statement) {
global $wpdb;
return $wpdb->posts .".post_type ASC";
}
add_filter('posts_orderby', 'order_posts_by_type');
$custom_query = new WP_Query( array(
'post_type' =>array( 'editorial','video','portfolio' ),
'posts_per_page' => '-1',
'orderby'=>'post_type',
'order' => 'ASC'
) );
remove_filter('posts_orderby', 'order_posts_by_type');
FWIW, I'd suggest changing 'posts_per_page=-1' to 'posts_per_page' => -1 to be consistent with your other arguments.

Resources