I've got a custom field on posts called number.
On the front-end in category.php, I need to total up the value of all posts in the current category that have number.
So for example, if the current category had 3 posts, and in each post the number values were 1, 5 and 10 respectively, then on the front-end it needs to display the total 16.
I'm not entirely sure where to start with this.
The loop I have at the moment:
<?php if ( have_posts() ) : ?>
<h1><?php printf( __( 'Category Archives: %s', 'twentythirteen' ), single_cat_title( '', false ) ); ?></h1>
<p></p>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // ?>
<?php endif; ?>
Any help is appreciated.
Thanks everyone. There was a lot there that helped me arrive at this solution (and a little help from Google):
<?php
$args = array(
'numberposts' => -1,
'offset' => 0,
'category' => $category
);
$numbers = get_posts( $args );
$total = 0;
foreach( $numbers as $numbersID ) {
$single = get_post_meta( $numbersID->ID, 'numbers', true );
$total += $single;
}
echo $total;
?>
The key was knowing to increment a variable += as #rnevius suggested, which I didn't know about.
A shorter version of what you're trying to achieve (which also reduces calls to the database) would look something like the following:
<p>
<?php
$total = 0;
foreach( $wp_query->posts as $number ) {
$total += get_post_meta( $number->ID, 'numbers', true );
}
echo $total;
?>
</p>
As mentioned in another comment, you don't need another loop, as your posts already exist in $wp_query.
Supposing you're using MySQL use SUM() Method to achive your goals. Query might look something like SELECT SUM(number) FROM category WHERE [drill down to a specific set of categories if neccesary];
Related
I have a static page with 10 containers located at different places in the index.php file, i want to show the 10 latest posts. I want to echo the data for a post (title,text,thumbnail,author,time) like this:
<div class="text">
<?php
echo '$textfrompost3';
?>
</div>
<div class="title">
<?php
echo '$titlefrompost3';
?>
</div>
...
...
My PHP file:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// insert values into variables
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Thanks in advance, i appreciate any help you can provide.
I'd simply replace your call to WP_Query with get_posts() which will provide an array of post objects.
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
) );
I've dropped order and orderby in the example above. While you may well have a different global setting for the number of posts to display, it's unlikely you're modifying the default ordering.
Documentation: https://codex.wordpress.org/Template_Tags/get_posts
So let's say I want to output the title of the third post, I could do the following:
// let's be sure we have a third post.
if ( isset( $latest_posts[2] ) ) {
echo $latest_posts[2]->post_title; // remember arrays have a zero-based index.
}
I'm trying to get the category name to show above posts. I'm customizing bones and am a little stuck.
Here's the code that has my posts displaying, and I have each in a category in the wp admin. I'm just not sure how to get the category to display above the appropriate posts.
Hope this makes sense. Thanks in advance!!
<?php
$args = array( 'post_type' => 'custom_type', 'posts_per_page' => 100 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php } else { ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php } ?>
It looks like you are doing a custom loop (as opposed to "The loop"), hence you'll need to pass the id of the post to get_the_category. However, judging by the docs, it seems that WP_Query->the_post() sets the global post.
So, if this does not work:
<h2><?php echo get_the_category() ?></h2>
You would do:
$postsQuery = new WP_Query( $args );
$posts = $postQuery->get_posts();
foreach ( $posts as $i => $e ) {
$category = get_the_category($e->ID);
}
https://developer.wordpress.org/reference/functions/get_the_category/
https://codex.wordpress.org/Class_Reference/WP_Query
https://codex.wordpress.org/The_Loop
You can use like <?php the_category();?>. This will returns the categories name with link. Hope this help you!
Hi I could not post in comments, but if above answer is working for you as you posted in comments but having issue with getting category as it give array due to many categories of a post, so you need to fetch first category from the array. Use below code snippet:
if ( ! empty( $category ) ) {
echo '' . esc_html( $category[0]->name ) . '';
}
I'm a bit of a wordpress newbie and was trying to find good solution what I was trying to do. Essentially I to get a list of categories that have a specific tag 'detination'. Once I do this I want to query for all posts with the above filter as well.
So What I have now is something like this:
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
$posts = get_posts($destCategories);
However, get_categories returns an Array of category specific information which can't really be used as a filter for get_posts. Does anyone have suggestions on how can solve this? I could essentially manually iterate through the destCategories array and construct a string of all the category names and use that as a filter in get posts, but I wanted to know if there's a more elegant solution that might be available.
Thanks for your help!
There's no tag argument for get_categories() : http://codex.wordpress.org/Function_Reference/get_categories#Parameters
I guess you could try passing in a taxonomy array, I don't know how it's gonna work out though.
If you want to get posts by tag, you could just use get_posts() : http://codex.wordpress.org/Template_Tags/get_posts#Taxonomy_Parameters
You could try something like this :
$args = array(
'post_type' => 'post',
'tag' => 'destination',
);
$query = new WP_Query( $args );
if ($query->have_posts()):
while ($query->have_posts()): $query->the_post(); ?>
<?php the_title(); ?>
<?php
endwhile;
endif;
?>
Ok so i ended up with something like this, which seems to work:
$query = get_posts( $myfilter );
$destCategories = get_categories( array('tag' => 'destination' , 'exclude' => '1') );
?>
<div id="main">
<div id="content clearfix">
<p>this is using front-page.php</p>
<?php //echo var_dump($destCategories) ?>
<?php
if ( have_posts( $myfilter ) ) {
foreach ( $destCategories as $category ) :
$posts = get_posts( array('category_name' => $category->slug) );
foreach ( $posts as $post ) :
?>
<h3> <?php echo the_title(); ?> </h3>
<?php
endforeach;
wp_reset_postdata();
//the_content();
endforeach;
?>
I'm having an issue with the listings in the WordPress site I'm working on.
I have three listings only showing up out of 6. I can't seem to figure out how to make all of them display. This is using the twentyeleven WordPress theme.
The arrows on the right are used to move the gallery back and forth. Only one more shows up on the right side.
Here's the code I believe is generating it.
<?php if ( have_posts() ) : ?>
<?php twentyeleven_content_nav( 'nav-above' ); ?>
<?php if ( is_home() ) {
query_posts($query_string . '&cat=-3');
}
?>
<?php
$page_name="Articles";
$page=get_page_by_title($page_name);
//echo $page->ID;
query_posts( 'cat=-1,-2' );
?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
Any help would be great, thanks.
Change your query_posts() function to the following:
query_posts( 'cat=-1,-2&posts_per_page=6' ); // You can change the post_per_page variable as needed
However, I would suggest using an $args array instead of a querystring to make your query. The same query would look like this:
$args = array(
'cat' => array( -1, -2 ),
'posts_per_page' => 6
);
query_posts($args);
It is much more readable and easier to update. Also, it's worth mentioning, you are adding a negative operator to your categories. In the query_posts function, that will exclude a category. You may only be getting 3 posts because you are excluding posts from your query.
Well I can't figure this one out...
I have this Wordpress I use as a photo gallery blog.
I have a basic setup using the main default loop for posts.
Like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; ?>
<b>Not Found</b>
<?php endif; ?>
In the sidebar and where ever, I want to appear random posts.
I've managed to do that. With this:
<?php query_posts($query_string . 'showposts=1&orderby=rand'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php endwhile; endif; ?>
It looks amazing! In theory.
There are duplicate posts all over the place. And that just looks stupid.
I have read lots of articles but I just can't seem to get it to work :(
Any help would be much appreciated.
Try this code for random post.
<ul>
<?php
$args = array( 'numberposts' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
<li><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
Or You can get help from this url mention below
http://codex.wordpress.org/Template_Tags/get_posts
After a good night of sleep, here's what I have done:
Creating array with post ID:
<?php $already_posted = array(); ?>
The Main loop where at the end I record the post ID to array:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//the post
<?php $already_posted[]= $post->ID; endwhile; ?>
<?php else : ?>
<b>Not Found</b>
<?php endif; ?>
And the random post code using post__not_in to avoid duplicates and again recording post ID:
<?php $args = array( 'numberposts' => 1, 'orderby' => 'rand', 'post__not_in' => $already_posted );
$rand_posts = get_posts( $args );
foreach( $rand_posts as $post ) : ?>
//the post
<?php $already_posted[]= $post->ID; endforeach; ?>
Works evertime!
You can do amazing stuff with this :)
Thanks to paislee and Arvind Pal for helping out.
Skip would-be duplicates by remembering displayed ID's from the first loop
$displayed = array(); // create an array that we'll use associatively
In your first loop, each time:
$displayed[get_the_ID()] = TRUE; // <-- save all post IDs in here
Change your random loop opening like this:
<?php if (have_posts()) : while (have_posts()) : the_post();
// skip post IDs you've already seen
if ($displayed[get_the_ID()]) continue;
?>
Due to randomness in the number of duplicates, you may want to alter your query so that it gets all posts, and change the second loop to break once the desired number of random posts is reached.
Notes
showposts is depracated. Replace showposts=1 with posts_per_page=-1