My posts get ordered by date which is picked by an advanced custom field datepicker. I want to use the regular Wordpress function references [the_title(), etc …] and the post related custom field's.
Right now the output of every loop is the the same. I read setup_postdata() can solve this issue and enable the use of the regular function references. I tried to apply it, but the output keeps being always the same. Thanks
<?php
global $posts;
$posts = get_posts(array(
'post_type' => 'post',
'meta_key' => 'release_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
$group_posts = array();
if( $posts ) {
foreach( $posts as $post ) {
$date = get_field('release_date', $post->ID, false);
$date = new DateTime($date);
$year = $date->format('Y');
$month = $date->format('F');
$group_posts[$year][$month][] = array($post, $date);
}
}
foreach ($group_posts as $yearKey => $years) {
foreach ($years as $monthKey => $months) {
echo '<li class="time">' . $monthKey . ' ' . $yearKey . '</li>';
foreach ($months as $postKey => $posts) {
setup_postdata($posts); ?>
<li class="item clearfix">
<!-- Wordpress Functions -->
<?php the_title();?>
<?php the_permalink();?>
<!-- Advanced Custom Fields -->
<?php the_field('blabla')?>
</li>
<?php
}
}
} wp_reset_postdata();
?>
You just need to add a line $post = $current_post; just before calling setup_postdata( $post ). See my example below to have a clear idea:
$posts = get_posts(array(.......));
// Call global $post variable
global $post;
// Loop through sorted posts and display using template tags
foreach( $posts as $current_post ) :
//the below line is what you missed!
$post = $current_post; // Set $post global variable to the current post object
setup_postdata( $post ); // Set up "environment" for template tags
// Use template tags normally
the_title();
the_post_thumbnail( 'featured-image-tiny' );
the_excerpt();
endforeach;
wp_reset_postdata();
For details please see this comment posted in the WP-Codex;
Related
I have tried to find a solution to something I need on the website (example here: https://www.baiweb.nl/). I have been looking for a way to show a custom number of posts per category AND sort them by date. I have managed to get a single post per category, but I can't seem to fix the rest. It sorts the posts itself by date now, but not all of them.
So, my questions are:
Is it possible to sort this loop, with all different categories, by date?
Is it possible to control the number of posts shown per category? This one is extra for me, not essential, but it would be nice.
Hope someone can help! Thanks a lot in advance for your time!
Here is my code used in the loop now:
<?php
$categories = get_categories();
$cats = array();
foreach($categories as $category) {
$cats[] = $category->name . ", ";
}
$exclude_posts = array();
foreach( $cats as $cat ) {
// build query argument
$query_args = array(
'category_name' => $cat,
'showposts' => 1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC'
);
// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
$query_args['post__not_in'] = $exclude_posts;
// do query
$query = new WP_Query( $query_args );
// check if query have any post
if ( $query->have_posts() ) {
// start loop
while ( $query->have_posts() ) {
// set post global
$query->the_post();
// add current post id to exclusion array
$exclude_posts[] = get_the_ID();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('loop');?>>
<div class="corner"><span><?php foreach((get_the_category()) as $category){ echo $category->name.'<br> '; } ?></span></div>
<!-- thumbnail -->
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="image">
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'medium', true);
if ( in_category('2') || in_category('32') ) {
echo '<div class="newstitle">' . the_title() . '</div>';
}
else {
if ( has_post_thumbnail()) {
echo "<div class='ctr-image test2' style='background-image: url(" . $thumb_url[0] . ")'></div>";
}
else {
echo '<div class="newstitle">' . the_title() . '</div>';
}
}
?>
</a>
<div class="content">
<span class="date"><?php the_time('j/m/Y'); ?></span>
<h3>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if ( in_category('2') || in_category('32') ) {}
else { echo the_title();} ?>
</a>
</h3>
<div class="text">
<?php html5wp_excerpt('html5wp_index'); // Build your custom callback length in functions.php ?>
</div>
</div>
</article>
<?php
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
} ?>
You were close. Your categories foreach loop has to englobe everything. Then we do a simple loop but we specify what arguments to use depending on each categories.
Here is our final result.
<?php
$categories = get_categories(); // ... get all our categories
foreach( $categories as $category ) { // ... start foreach categories
if ( $category->name == 'Mercury' ) { // ... if our category name is 'Mercury'
$posts_per_page = 1; // ... 1 post per page if our category is named 'Mercury'
} else { // ... else, for all other categories
$posts_per_page = 3; // ... 3 posts per page
};
$args = array( // ... all our arguments
'posts_per_page' => $posts_per_page, // ... 1 or 3 posts per page depending on our categories
'post_type' => 'post',
'category_name' => $category->name,
'post_status' => 'publish',
'orderby' => 'date', // ... order by date
'order' => 'ASC', // ... most recent first
);
$query = new WP_Query( $args ); // .. start a new loop
if( $query->have_posts() ):
echo '<section>';
echo '<h1>' . $category->name . '</h1>'; // ... only display our section IF a post exist in the category
while( $query->have_posts() ): $query->the_post();
echo '<article>'; // ... our post template
the_title( '<h4>', '</h4>' );
echo '</article>';
endwhile;
echo '</section>';
endif;
wp_reset_postdata(); // ... After looping through a separate query, this function restores the $post global to the current post in the main query.
}; // ... end foreach categories
?>
Thank you so much for your quick and comprehensive answer! The part of the number of categories is working very well, however, the outcome is slightly different than I want. It may be that my question was not clear, apologies for that.
What you have already made beautiful is all the categories with posts that belong to it. What I want to achieve is that all messages are mixed up, sorted by date and, what was already done here, I can indicate how many posts are in per category. Is that even possible?
Some screenshotsof the outcome of your code:
This is what I am trying, except now I need to sort this by date:
This is the code slightly adjusted:
<?php
$categories = get_categories(); // ... get all our categories
foreach( $categories as $category ) { // ... start foreach categories
if ( $category->name == 'Mercury' ) { // ... if our category name is 'Mercury'
$posts_per_page = 1; // ... 1 post per page if our category is named 'Mercury'
} else { // ... else, for all other categories
$posts_per_page = 2; // ... 3 posts per page
};
$args = array( // ... all our arguments
'posts_per_page' => $posts_per_page, // ... 1 or 3 posts per page depending on our categories
'post_type' => 'post',
'category_name' => $category->name,
'post_status' => 'publish',
'orderby' => 'date', // ... order by date
'order' => 'ASC', // ... most recent first
);
$query = new WP_Query( $args ); // .. start a new loop
if( $query->have_posts() ):
// echo '<section>';
// echo '<h1>' . $category->name . '</h1>'; // ... only display our section IF a post exist in the category
while( $query->have_posts() ): $query->the_post();
echo '<article>'; // ... our post template
echo '<h1>' . $category->name . '</h1>';
the_time('j/m/Y');
the_title( '<h4>', '</h4>' );
echo '</article>';
endwhile;
// echo '</section>';
endif;
wp_reset_postdata(); // ... After looping through a separate query, this function restores the $post global to the current post in the main query.
}; // ... end foreach categories ?>
Hope this is clear, thanks again!
I'm using an AJAX search to pull 3 custom post types from wordpress (post, guides, advice). As you can see in my if while loop, the results show which results are which but I'm trying to section them out individually so it will show something like this:
section 1
blog posts
section 2
guides
The problem seems to be that I need to edit the if while loop because adding anything inside that loop will just cause it to be in that loop. Does anyone know the best way to modify the if while loop to achieve this?
function data_fetch(){
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
's' => esc_attr( $_POST['keyword'] ),
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
)
);
global $post;
if( $the_query->have_posts()) :
while( $the_query->have_posts() ): $the_query->the_post(); ?>
<?php $type = get_post_type();
$term = $_POST['keyword'];
$i++;
$total = $the_query->found_posts;
?>
<span class="search-title">
<?php if ($type == 'post'):?>
<?php echo 'Article: ';?>
<?php elseif($type == 'guide' ):?>
<?php echo 'Guide: ';?>
<?php elseif($type == 'advice' ):?>
<?php echo 'advice: ';?>
<?php endif;?>
<?php the_title();?><br>
</span>
<?php endwhile; ?>
<?php
wp_reset_postdata();
else:
echo '<h3>No Results Found</h3>';
endif;
die();
}
If I were you, I'd probably just do three separate queries. They seem simple enough that it shouldn't cause any issues at all. Otherwise you have to either sort through or reorder the WP_Query results somehow.
If you're set on the single query, however - since these are such short HTML strings, I'd probably just control the output with the Output Buffer.
Basically you can create an associative array, and add the HTML strings to the appropriate keys. You could get a bit more elaborate and have them be nested arrays, but since your output is so light, you can probably get by just by having HTML strings as the values for the keys.
I took the liberty of cleaning up your code a little bit and removing some unused variables, etc.
function data_fetch(){
// Build the Query Arguments
$the_query = new WP_Query( array(
's' => esc_attr($_POST['keyword']),
'posts_per_page' => 6,
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
) );
// Do we have posts?
if( $the_query->have_posts()){
// We do. Start an array that will fill with HTML from the output buffer
$output = array(
'post' => '',
'guides' => '',
'advice' => '',
);
// Loop through the posts
while( $the_query->have_posts() ){
$the_query->the_post();
ob_start(); // Turn on the output buffer
$type = get_post_type(); ?>
<span class="search-title">
<?php echo ($type == 'post') ? 'Article: ' : ucwords($type).': '; // Output "Articles: " for posts, or just capitalize the other post type names ?>
<?php the_title();?><br>
</span>
<?php $output[$type] .= ob_get_clean(); // Add the HTML output from the buffer to our array
}
wp_reset_postdata();
// Here we have an array with 3 HTML strings that we can output wherever
echo $output['post'];
echo $output['guides'];
echo $output['advice'];
} else {
echo '<h3>No Results Found</h3>';
}
}
if I understood your concern, I propose to divide your code by creating a generic class.
and you make calls to the different Post Type :
class Custom_Query {
public $post_type;
public $key_word;
public function __construct($post_type, $keyword) {
$this->post_type = $post_type;
$this->key_word = $keyword;
}
public function getResult() {
echo '<h1>Article : ' . $this->post_type . '</h1>';
$the_query = new WP_Query(
array(
'posts_per_page' => 6,
's' => esc_attr($this->key_word),
'post_type' => array($this->post_type),
'post_status' => 'publish'
)
);
if ($the_query->have_posts()):
while ($the_query->have_posts()): $the_query->the_post();
echo '<span class="search-title">';
echo '' . the_title() . '<br>';
echo '</span>';
endwhile;
else:
echo '<h3>No Results Found</h3>';
endif;
wp_reset_postdata();
}
}
global $post;
$type = get_post_type();
$term = $_POST['keyword'];
$article = new Custom_Query($type, $term);
$article->getResult();
You were on the right track initially. You can just use the standard post objects to check for post type. You can adjust to fit your needs, but try something like this:
<?php
query = new WP_Query( array(
's' => esc_attr($_POST['keyword']),
'posts_per_page' => 6,
'post_type' => array('post' , 'guides', 'advice'),
'post_status' => 'publish'
) );
if ($query->have_posts()) {
while ($query->have_posts()):
$query->the_post();
?>
<div class="posts">
<?php
if ($query->post->post_type === 'post') {
// add your content
} else if ($query->post->post_type === 'guides' {
// add your content
} else {
// add your content
}
?>
</div>
<?php endwhile;
} else {
// no posts found
}
I'm trying to get a category and loop through its sub categories getting one post from each of those sub-categories. Below is my code:
<?
$homepage_cat = get_category_by_slug( 'home-page-slider' );
$id = $homepage_cat->cat_ID;
print($id);
$sub_cat = get_categories('hide_empty=0&child_of=' . $id);
print_r($sub_cat);
foreach ($sub_cat as $key => $cat)
{
echo $cat->term_id;
query_posts('cat=' . $cat->term_id);
if ( have_posts() )
{ echo '<h1> HELL YEAH </h1>';
while ( have_posts() )
{
echo '<h1>' get_the_title(); '</h1>';
} // end while
} // end if
} //end foreach
?>
The code is not returing any posts as HELL YEAH is not being echoed. Can anyone suggest a solution?
Use get_posts() not query_posts, it is better for these kinds of situations.
$args = array('posts_per_page' => 1, 'category' => $cat->term_id);
$posts = get_posts($args);
foreach($posts as $post) : setup_postdata( $post ) ?>
<h1><?php get_the_title(); ?></h1>
<?php endforeach; ?>
There are quite a few problems here
First of all, never use query_posts to construct custom queries. It breaks the main query, is unreliable and outright fails in most cases in pagination
Secondly, you have to reset your postdata after every custom query
Thirdly, never use short tags. Always use full tag ie <?php and not just <?
Lastly, you are missing the_post() which should return post objects
Your query should look something like this
<?php
$homepage_cat = get_category_by_slug( 'home-page-slider' );
$id = $homepage_cat->cat_ID;
print($id);
$sub_cat = get_categories('posts_per_page=1&hide_empty=0&child_of=' . $id);
print_r($sub_cat);
foreach ($sub_cat as $key => $cat)
{
echo $cat->cat_ID;
$q = new WP_Query('cat=' . $cat->cat_ID);
if ( $q->have_posts() )
{ echo '<h1> HELL YEAH </h1>';
while ( $q->have_posts() )
{
$q->the_post();
echo '<h1>' get_the_title(); '</h1>';
} // end while
} // end if
wp_reset_postdata();
} //end foreach
?>
Replace this
$post_args = array(
'showposts' => 1,
'cat' => $cat->term_id
);
with this.
$post_args = array(
'posts_per_page' => 1,
'category' => $cat->term_id
);
I hope it'll work.
I'm trying to query wordpress post by using query_posts and trying to save them in an array so that I get retrieve the post from array. This is what I'm doing,
$posts= array();
$args = array('posts_per_page' =>3,'cat' => 3 );
$posts[] = query_posts( $args );
global $post;
if ( ! empty($posts) ) :
foreach ($posts as $post) {
setup_postdata($post);
echo get_the_title();
}
wp_reset_postdata();
endif;
wp_reset_query();
When I run this script it shows a post which is not in cat 3. but if I do print_r($post) it shows the correct three posts. Any idea of where I'm getting wrong?
No need to declare it as an array:
global $post;
$args = array('posts_per_page' => 3,'cat' => 3);
$query = query_posts($args);
foreach ($query as $post) {
setup_postdata($post);
the_title();
}
wp_reset_query();
This one should work also WP_Query or get_posts() is preferred method for secondary queries.
global $post;
$posts_args = array('cat' => 3, 'posts_per_page' => 3);
$posts_query = new WP_Query($posts_args);
$posts_arr = $posts_query->get_posts();
if ( ! empty($posts_arr) ) :
foreach ($posts_arr as $post) {
setup_postdata($post);
echo get_the_title();
}
wp_reset_postdata();
endif;
wp_reset_query();
Remember that query_posts modifies the main loop, so probably the post that is not from cat = 3 is from the main loop. Try to create a new query with get_posts.
$posts = get_posts('posts_per_page=3&cat=3');
global $post;
if (!empty($posts)):
foreach ($posts as $post):
setup_postdata($post);
echo get_the_title();
endforeach;
wp_reset_postdata();
endif;
I have a Custom post type of "events" and I have a custom metabox for the date of the event. All events added with all dates recorded into database in srtotime() UNIX format.
When list of events is queried I need to separate them by week. So.. "week of 4/23" then list the events for that week only.. followed by "Week of 4/30" with the events for that week listed.
All of this needs to be dynamic and logically detect when each week begins and compare that to the date that is saved in the database for the returned events, thus grouping them into "weeks."
I hope that all makes sense. I am just having trouble finding resources for this type of functionality, and I am uncertain of the logic needed and what WP might already have I could use.
As of now the results are being pulled using a custom Wordpress loop, but I could use SQL if needed.
Thanks in advance for helping any way you can!
*UPDATED CODE
<?php
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
echo '<pre>';
print_r($loop);
echo '</pre>';
$groups = array();
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$groups['Week of ' . $post->post_date][] = $post;
// Get event dates from WP metabox data (returns as string)
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
$longweekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
// Reformat dates
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate);
$prettyweekof = date("m/d", $longweekof);
// Get Custom Meta Terms/Catagories/taxonomies
$the_venue = get_the_term_list( $post->ID, 'venue' );
$event_age = get_the_term_list( $post->ID, 'event_age' );
// Get Custom Metabox Data - URL for Buy Tickets
$buytix = get_post_meta( $post->ID, 'buytix', true );
}
}
?>
<h1>Groups of posts</h1>
<?php echo '<pre>'; print_r($groups); echo '</pre>';?>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>
<ul>
<?php foreach ($rows as $post) : setup_postdata($post) ?>
<?php echo ' <h1>POST START</h1> <pre>';
print_r($post);
echo '</pre> <h1>POST END</h1>'; ?>
<li><?php the_title() ?> <?php echo $prettystartdate; ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
When I need to group elements I normally use an associative array where the key is the thing to group by. So in your case the array would look something like:
array(
'Week of 4/30' => array(array of posts)
);
An easy way to do this (once you have all the posts) is like this:
$groups = array();
foreach ($posts as $post) {
$groups['Week of ' . date('m/d', $post->pub_date)][] = $post;
}
And then you can just:
foreach ($groups as $title => $posts) {
echo "<h2>$title</h2>";
foreach ($posts as $post) ...
}
Edit: Here's what a more complete code (including WP query etc) would look:
<?php
# Get all posts
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
$groups = array();
# Create groups of posts
if ($loop->have_posts) {
while ($loop->have_posts()) {
$loop->the_post();
$groups['Week of ' . date('m/d', $post->tf_events_weekof)][] = $post;
}
}
?>
<h1>Groups of posts</h1>
<?php foreach ($groups as $week => $rows) : ?>
<h2><?php echo $week ?></h2>
<ul>
<?php foreach ($rows as $post) : setup_postdata($post) ?>
<li><?php the_title() ?></li>
<?php endforeach ?>
</ul>
<?php endforeach ?>
Here is rough solution that is working for me.
<?php
$loop = new WP_Query(array(
'post_type' => 'tf_events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'meta_key' => 'tf_events_startdate',
'order' => 'ASC'
));
$groups = array();
$heading = '';
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
//set up pretty week
$weekof = get_post_meta($post->ID, 'tf_events_weekof', true); //get Week Of date meta data
$longstartdate = get_post_meta($post->ID, 'tf_events_startdate', true); //get start date meta data
$longenddate = get_post_meta($post->ID, 'tf_events_enddate', true); //get start date meta data
//format dates
$prettyweekof = date("m/d", $weekof);
$prettystartdate = date("D. M. j, Y", $longstartdate);
$prettyenddate = date("l, F j, Y", $longenddate);
//set info on objects
$post->prettyweekof = $prettyweekof;
$post->pretty_startdate = $prettystartdate;
$post->pretty_enddate = $prettyenddate;
//Echo heading when need to
if($post->prettyweekof !== $heading)
{
$heading = $post->prettyweekof;
echo '<h2>'.$heading.'</h2>';
}
//list events
echo $post->post_title.' | '.$post->pretty_startdate.' - '.$post->pretty_enddate.'<br />';
}
}
?>