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; ?>
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 hace 2 sections on my site: first is for popular posts (based on views) and the second section is for the recent posts.
If the post is already in the popular posts section, I don't want it to be displayed in the "Recent posts" section. Below is my code. In the first loop I've created an array to store all post ID's which are in that section. In the second loop I check if the id is in that array (may be not the best solution).
For some reason it only works with the first duplicate, even though $cont becomes true required amount of times(I checked with echo). So what gives?
<?php
$popularpost = new WP_Query( array( 'posts_per_page' => 4, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
$counter=0;
$post_ids = array();
while ( $popularpost->have_posts() ) : $popularpost->the_post();
$postID = get_the_ID();
$post_ids[$counter] = $postID;
?>
<?php the_title(); ?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php $myquery = new WP_Query('posts_per_page=6');
while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<?php $post_id = get_the_ID(); ?>
<?php $post_ids_length = count($post_ids); ?>
<?php for ($i=0; $i < $post_ids_length; $i++) {
if ($post_id == $post_ids[$i]) {
$cont = "true";
} else {
$cont = "false";
}
} ?>
<?php if ($cont == "true") {
continue;
} ?>
<?php the_title(); ?>
<?php endwhile; ?>
Update your Second Query as below:
$args2 = array('post__not_in' => $post_ids,'posts_per_page' => 6 );
$myquery = new WP_query($args2);
And then just iterate over the result using the While loop.
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.
In Wordpress, I want to show the 2 latest posts along with the post thumbnail for just the first post.
I have been playing around with the code below, but an image always ends up being shown for the first post as well as the second, when I only want to show an image for the first.
<?php
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC',
'child_of' => 0
);
$post_args = array(
'numberposts' => 2,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php the_post_thumbnail('blog_post_image'); ?>
<?php
}
}
?>
You're sort of missing any condition that would allow you to selectively display the image.
<?php
foreach($posts as $key=>$post) {
the_title();
if (0 == $key) {
the_post_thumbnail('blog_post_image');
}
}
Assuming $posts is a 0-based enumerated array. Notice the addition of $key to the foreach, as well as the if before printing the thumbnail
<?php $loop = 1; ?>
<?php foreach($posts as $post): ?>
<?php the_title(); ?>
<?php if($loop == 1): ?>
<?php the_post_thumbnail('blog_post_image'); ?>
<?php endif; ?>
<?php $loop++; endforeach; ?>