I've got the following code which will display 4 random posts from the defined category. However, that's a little too random, I need it to display the 4 most recent, and then randomise them in particular each time the page is refreshed.
Otherwise I just end up getting articles which could date back over 2 years ago appear on the homepage.
<div class="article-block-v1">
<?php
//get terms (category ids 11,2,33,34), then display one post in each term
$taxonomy = 'category';// e.g. post_tag, category
$param_type = 'category__in'; // e.g. tag__in, category__in
$term_args=array(
'include' => '1459',
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 4,
'orderby' => 'rand',
);
$my_query = null;
$my_query = new WP_Query($args);
$i = 1;
if( $my_query->have_posts() ) {
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php if ($i == 1): ?>
<div class="article-block-v1">
<div class="category-label-large category-news-analysis">
<p><?php echo $term->name; ?></p>
</div>
<div class="view2 third-effect">
<?php the_post_thumbnail(); ?>
<div class="mask">
</i>
</div>
</div>
<ul class="homepagewhitebg">
<li><h5><?php the_title(); ?></h5></li>
</ul>
</div>
<?php else: ?>
<li><p><?php the_title(); ?></p></li>
<?php endif; ?>
<?php
$i++;
endwhile;
echo '</ul>';
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
Demo 1 (4 Most Recent) - Regular without random orderby
Article #1001
Article #1002
Article #1003
Article #1004
Demo 2 (4 Most Recent - But with order by random on those 4)
Article #1003
Article #1001
Article #1004
Article #1002
UPDATE
I'm also trying this now, however it's not taking into account the category I've defined, nor is it randomising across the 4 displayed:
<div class="article-block-v1">
<?php
$number = "4";
$posts = "SELECT * from $wpdb->posts WHERE post_type='post' ORDER BY post_date DESC LIMIT $number";
$postX = array();
$postresult = mysql_query($posts) or die(mysql_error());
while ($row = mysql_fetch_array($postresult)) {
$postX[] = $row[0];
}
$ids = shuffle($postX);
$ids = implode(',', $postX);
echo $ids;
?>
<?php
$args = array(
'posts_per_page'=> $number,
'post__in' => explode(',', $ids),
'include' => '1451',
'orderby' => 'rand'
);
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
You can use shuffle(). You're going to want to get the 4 most recent posts (in order) before shuffling them. Quite simply, you could do something like the following:
// Get the 4 most recent posts
$args = array( 'numberposts' => 4 );
$recent_posts = wp_get_recent_posts( $args );
// Shuffle them
shuffle($recent_posts)
foreach( $recent_posts as $recent ){
// Do something with the $recent post
}
You may need to pass additional $args to the function, for your specific constraints.
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 have a custom loop that pulls all the child pages of the current page, and displays them. However, I need to order this query by the $first_row_image variable that is pulled inside the loop.
This is done by grabbing the title row at the end() of an array (The array being a repeater field in Advanced Custom Fields).
I know my below code isn't right, but I can't work out how to do it.
<?php function flo_add_child_pages() {
global $post;
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'cat' => '3368',
'posts_per_page' => -1,
'meta_key' => $first_row_image,
'orderby' => 'meta_value',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="child-flex">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$rows = get_field('breadcrumbs' ); // get all the rows
$first_row = end($rows); // get the first row
$first_row_image = $first_row['title' ]; // get the sub field value
?>
<?php if($first_row_image) : ?>
<a href="<?php the_permalink(); ?>" rel="post-<?php the_ID(); ?>" class="one-half">
<?php echo $first_row_image; ?><i class="fa fa-angle-right rotate-icon" style="float: right; margin-top: 5px;"></i>
</a>
<?php endif;?>
<?php endwhile; ?>
</div><?php wp_reset_postdata(); ?>
<?php endif;
}
In my experience, it's usually better to do complex sorting after the query instead of as part of the query.
For one thing, you defined 'meta_key' => $first_row_image, but $first_row_image is undefined at that point, so the query can't execute on it. For another thing, given the way that ACF stores repeater values, it'd be almost impossible to query on them.
I'd do it this way:
Run the query
Check to see if you have posts
Use usort to write a custom sorting function on the $query->posts array.
<?php
function reorder_by_last_title($page1, $page2) {
$page1_rows = get_field('breadcrumbs', $page1['ID']); // get all the rows
$page1_first_row = end($page1_rows); // get the first row
$page1_first_row_image = $page1_first_row['title']; // get the sub field value
$page2_rows = get_field('breadcrumbs', $page2['ID']);
$page2_first_row = end($page2_rows);
$page2_first_row_image = $page2_first_row['title'];
return $page1_first_row_image > $page2_first_row_image;
}
function flo_add_child_pages() {
global $post;
$args = array(
'post_type' => 'page',
'post_parent' => $post->ID,
'cat' => '3368', // Not sure why this is needed-- did you enable categories for pages?
'posts_per_page' => 500, // Use a big number instead of -1
);
$child_pages_query = new WP_Query($args);
if ($child_pages_query->have_posts()) :
// Reorder child pages by last breadcrumbs title.
usort($child_pages_query->posts, 'reorder_by_last_title' );
?>
<div class="child-flex">
<?php while ($child_pages_query->have_posts()) : $child_pages_query->the_post(); ?>
<?php
$rows = get_field('breadcrumbs'); // get all the rows
$first_row = end($rows); // get the first row
$first_row_image = $first_row['title']; // get the sub field value
?>
<?php if ($first_row_image) : ?>
<a href="<?php the_permalink(); ?>" rel="post-<?php the_ID(); ?>" class="one-half">
<?php echo $first_row_image; ?><i class="fa fa-angle-right rotate-icon"
style="float: right; margin-top: 5px;"></i>
</a>
<?php endif; ?>
<?php endwhile; ?>
</div><?php wp_reset_postdata(); ?>
<?php endif;
}
I have a category menu that filters the posts shown in the page. My problem is that instead of having an item per category in the menu, I see duplicates of the same Item. It seems like I have as much duplicates as the number of the posts under that category.
Link to the page where the navigation is: http://www.confesercenti.pistoia.it/connessioni/
You can see it under "Filtra le aziende per settore economico". For example, you will notice "COMUNICAZIONE" is repeated five times.
This is the code:
<div id="filters" class="settore_dropdown_wrapper button-group">
<p>Filtra le aziende per settore economico</p>
<ul id="port-list" class="port-filter">
<li><a class="active" data-filter="*">Tutti</a></li>
<?php
$main_convenzione = new WP_Query(array(
'post_type' => 'azienda',
'meta_key' => 'convenzioni_attive',
'meta_value' => 1
));
if ($main_convenzione->have_posts()) : while($main_convenzione->have_posts()) : $main_convenzione->the_post();
// while($main->have_posts()) : $main->the_post();
global $post;
$post_id = $post->ID;
// $terms_list = get_the_terms( get_the_ID(), 'settore' );
$terms_list = get_the_terms( $post_id, 'settore');
// $args = array(
// 'post_type' => 'azienda', // filter by the post type progetto
// 'taxonomy' => 'settore',
// 'hide_empty' => true
// );
// $terms = get_terms($args); // Get all terms of a taxonomy
foreach ( $terms_list as $term_single ) {
$term_name = $term_single->name;
$term_slug = $term_single->slug; ?>
<li><a data-filter=".<?php echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term_name)); ?>">
<?php echo esc_attr($term_name); ?></a></li>
<?php }
endwhile; endif; wp_reset_postdata(); ?>
</ul><!--port-list-->
</div><!--filters-->
As stated above it seems like it shows as much duplicates as the number of the posts under that category. My goal is to have one item per category instead of having a al those duplicates
You need to save the filters you need instead of showing them directly. You can use in_array to check if a value is in your array. Something like would work:
<div id="filters" class="settore_dropdown_wrapper button-group">
<p>Filtra le aziende per settore economico</p>
<ul id="port-list" class="port-filter">
<li><a class="active" data-filter="*">Tutti</a></li>
<?php
$main_convenzione = new WP_Query(array(
'post_type' => 'azienda',
'meta_key' => 'convenzioni_attive',
'meta_value' => 1
));
if ($main_convenzione->have_posts()) : while($main_convenzione->have_posts()) : $main_convenzione->the_post();
global $post;
$terms_list = get_the_terms( $post->ID, 'settore');
$terms = [];
foreach ( $terms_list as $term_single ) {
if(!in_array($term_single->name, $terms)) {
$terms[] = $term_single->name;
};
};
endif;
?>
<?php foreach($terms as $term): ?>
<li><a data-filter=".<?php echo strtolower(preg_replace('/[^a-zA-Z]+/', '-', $term)); ?>"><?php echo esc_attr($term); ?></a></li>
<?php endforeach; wp_reset_postdata(); ?>
</ul><!--port-list-->
</div><!--filters-->
Wordpress team members have a select in admin that lets you pick a number to display the team in a certain order on Archive page. The Select returns a value of the number, which i retrieve.
However, I want to display all the members that have the value of 1 first, and any members after that show up below. At the moment, the IF statements only shows the content if they exist (obviously), i would like to however make it work somehow so it shows Members with value 1 first, then members with value 2. Thank you!
$images = get_field('image');
$titles = get_the_title();
$positions = get_field('position');
$link = get_permalink($post->ID);
if ($positions[0] == 1) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
if ($positions[0] == 2) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
If you want to sort the $positions array you could use
sort($positions);
You you can easily sort by a meta value with get_posts()...
<?php
$args = array(
'post_type' => 'team_members',
'posts_per_page' => -1,
'meta_key' => 'position',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$posts = get_posts( $args );
?>
<?php if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li class='employee'>
<img src='$images'>
<h2>$titles</h2>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I am trying to create a system which fetches post from 2 post types and i want to display the posts order by their post type. I am trying to first show the posts from one post type and then another post type but the query i used mixes it all. Can any one suggest a good solution for this. Here is my code.
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array('review','directory'),
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => '4',
'paged' => $paged ,
'tax_query' => array( array(
'taxonomy' => 'Reviews',
'field' => 'id',
'terms' => $cat_id
), ),
);
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
$product_terms = wp_get_post_terms(get_the_ID(), 'Reviews', array("fields" => "all", "order" => "DESC"));
$postype=get_post_type( get_the_ID() );
if($postype=='review')
{
?>
<div class="review-post">
<h3><?php the_title(); ?></h3>
<div class="review-cat-new">
<?php echo get_the_term_list( $post->ID, 'Reviews', 'Category: ', ', ', '' ); ?>
</div>
<?php
if(get_field('see_it'))
$seeit_text= get_field('see_it');
if(get_field('skip_it'))
$skipit_text= get_field('skip_it');
?>
<div class="see-skip">
<p class="see-it"><span>See it:</span>
<?php echo $seeit_text; ?>
</p>
<p class="skip-it"><span>Skip it:</span>
<?php echo $skipit_text; ?>
</p>
</div>
<?php echo custom_field_excerpt(); ?>
</div>
<?php }
else
{
?>
<div class="review-post">
<h3><?php the_title(); ?></h3>
<div class="review-cat-new">
<?php echo get_the_term_list( get_the_ID(), 'Reviews', 'Category: ', ', ', '' ); ?>
</div>
<?php echo the_field('enter_content_direc'); ?>
<?php
if(get_field('enter_textdirec'))
$text= get_field('enter_textdirec');
if(get_field('enter_linkdirec'))
$textlink= get_field('enter_linkdirec');
?>
<div class="see-skip">
<p class="see-it direc"><span><a target="_blank" style="color:#5D6D71; text-transform: lowercase;" href="<?php echo $textlink;?>"><?php echo $textlink;?> </a></span>
</p>
</div>
</div>
<?php }
endwhile;
echo '<div class="paging">';
wp_pagenavi();
echo '</div>';
endif;
So is there a way so that i can first show the posts form reviews and then directory?
Since Wordpress 4.0 you can just pass the option type (post_type) to the orderby parameter in the WP_Query object.
orderby (string | array) - Sort retrieved posts by parameter. To order by post type just pass:
'type' - Order by post type (available since Version 4.0). ('post_type' is also accepted.)
As seen in the docs https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters , you can use the regular wp query by passing an array to 'orderby' :
$wp_query = new WP_Query(
array(
's' => $search,
'orderby'=> array('type'=>'DESC', 'title'=>'ASC'),
'paged'=>$paged
)
);
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// do your loop stufff here
}
}
Finally i got the solution and it can be done using a filter.
The solution looks like this.
add_filter( 'posts_request' , 'modify_request' );
function modify_request( $query) {
global $wpdb;
if(strstr($query,"post_type IN ('review', 'directory')")){
$where = str_replace("ORDER BY {$wpdb->posts}.post_date","ORDER BY {$wpdb->posts}.post_type",$query);
}
return $where;
}
Order per post types with custom post type order:
add_filter('pre_get_posts', function ($query) {
if ($query->is_search && !is_admin()) :
$query->set('post_type', [ 'cpt-1', 'cpt-2', 'post', 'cpt-3']);
endif;
return $query;
});
add_filter('posts_orderby', function ( $order ) {
if ( ! is_admin() ) :
if ( is_search() && is_main_query() ) :
global $wpdb;
$order = "FIELD( post_type, 'cpt-1', 'cpt-2', 'post' ), {$wpdb->posts}.post_modified DESC";
endif;
// Disable this filter for future queries!
remove_filter( current_filter(), __FUNCTION__);
endif;
return $order;
});
First solution which comes to my mind is to get posts only for first category. Reset query (wp_reset_query()) and get them again for second category. You need to double up your code of course, but I don't see any other solution - it's WordPress - you're very limited.
Second solution is to query database directly like this:
$result = mysql_query('SELECT *
FROM `wp_posts`
WHERE post_type = "page"
OR post_type = "post"
ORDER BY post_type ASC , post_date DESC ');
$posts = array();
if($result) {
while ($row = mysql_fetch_assoc($result)) {
$posts[] = $row;
}
}
mysql_free_result($result);
echo('<pre>');
var_dump($posts);
echo('</pre>');
die();
Notice that I've used post and page types as these are default, and exist in my WP installation. But you can use different. You can play with this query as much as you want.