Archive Template in Wordpress - php

I want to create an archive page template for Wordpress that will look like this:
August 2009
Post 4
Post 3
Post 2
Post 1
July 2009
Post 2
Post 1
So, basically, I want all the posts from the blog, ordered descending by date and grouped by month.
Can someone provide me the PHP code for this?
Thanks!
PS: Wordpress version is 2.8.2

This is a function I created a while back. It basically does what you want to do, but it's not a template. Maybe you can adapt it.
<?php
/**
* Displays a condensed list of the posts grouped by month/year.
*
* #param $order The order of the posts. Either 'DESC' or 'ASC', case sensitive.
* #param $date_prefix Whether to prefix the posts with the month/date.
* #param $display Whether to display the results or return it as a String.
*/
function condensed_post_list($order='DESC', $date_prefix=true, $display=true){
global $wpdb;
if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
$query = "SELECT ID, post_title, post_date FROM $wpdb->posts ".
"WHERE post_type='post' AND post_status = 'publish' ".
"ORDER BY post_date $order";
$results = $wpdb->get_results( $query );
ob_start();
$current_month = '';
foreach( $results as $result ) {
if( $current_month != mysql2date('F Y', $result->post_date)) {
if( $current_month ) echo '</ul>';
$current_month = mysql2date('F Y', $result->post_date );
echo '<h2>'.$current_month.'</h2>';
echo '<ul>';
}
echo '<li>';
echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
echo '<a href="'.get_permalink($result->ID).'">';
echo $result->post_title.'</a></li>';
}
if( $current_month ) echo '</ul>';
if( $display ) {
ob_end_flush();
} else {
return ob_get_clean();
}
}
?>

I used the above function, but replaced the SQL query with:
$results = query_posts('post_type=post&post_status=publish&cat=3');
This allowed me to use the excellent function #scompt.com wrote, but limit it to a single blog category.

Thanks a lot of the help. This is what I used with the code above.
function condensed_post_list($order='DESC', $date_prefix=true, $display=true)
{
if( !in_array($order, array('DESC','ASC' ) ) ) $order = 'DESC';
$args = array(
'numberposts' => -1,
'orderby' => 'post_date',
'post_type' => 'post',
'post_status' => 'publish');
$results = get_posts($args);
ob_start();
$current_month = '';
foreach( $results as $result ) {
if( $current_month != mysql2date('F Y', $result->post_date)) {
if( $current_month ) echo '</ul>';
$current_month = mysql2date('F Y', $result->post_date );
echo '<h2>'.$current_month.'</h2>';
echo '<ul>';
}
echo '<li>';
echo ($date_prefix ? mysql2date('M j: ', $result->post_date) : '');
echo '<a href="'.get_permalink($result->ID).'">';
echo $result->post_title.'</a></li>';
}
if( $current_month ) echo '</ul>';
if( $display ) {
ob_end_flush();
}
else {
return ob_get_clean();
}
}

Related

How can I calculate the sum of a custom-field from several posts?

I run a running/biking website with wordpress. Now I would like to create a PHP query that returns the posts of the last 30 days. I have done that.
However, I would also like to calculate the total distance covered during these 30 days. The distance is in a custom-field called "distance".
I think it should be possible to do this somehow with "sum" - but I can't manage it. Does anyone have any ideas?
`
<?php
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
`
in your functions.php
function get_total_distance(){
$get_posts = get_posts(
array(
'numberposts' => -1,
'date_query' => array(
'after' => date('Y-m-d', strtotime('-30 days'))
)
)
);
$total = 0;
if( $get_posts ){
foreach( $get_posts as $post ){
setup_postdata( $post );
$distance = get_post_meta($post->ID, 'distance');
if( $distance ){
$total += $distance;
}
}
}
return $total;
}
in your theme:
echo get_total_distance();

Advanced Custom Fields Wordpress Repeater Field Order By Date

I am using advanced custom fields pro to store and display information on estate sale dates. These are held in a repeater field, each row in the field has a start time, end time and a date. I need to display the information ordered by the date in the repeater field row. Currently it is ordered by date modified. I have tried several solutions but none seem to work for what I need to do.
This is what I have currently:
<?php
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
endwhile;
wp_reset_query();
?>
The most common answer when searching for a solution is to do something like this:
$latestes = new WP_Query(
array(
'post_type' => 'estate-sales',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'modified',
'order' => 'DESC'
)
);
while ( $latestes->have_posts() ) : $latestes->the_post();
$repeater = get_field('estate_sale_dates');
foreach( $repeater as $key => $row )
{
$column_id[ $key ] = $row['es-date'];
}
array_multisort( $column_id, SORT_ASC, $repeater );
foreach( $repeater as $row ) {
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo ' ';
$rowses = get_field('estate_sale_dates');
$first_row = $rowses[0];
$first_row_date = $first_row['es-date'];
$first_row_start = $first_row['es-start-time'];
$first_row_end = $first_row['es-end-time'];
echo ' - ';
if ($first_row_date) {
echo date('F j, Y',strtotime($first_row_date));
}
else {
echo 'Sale Dates TBA';
}
}
endwhile;
wp_reset_query();
?>
However, when I try that, it lists each sale 3 times, does not order by the date at all, and if no date is entered (which needs to just default to Sale Dates TBA) it throws an error.
Any help greatly appreciated.
Note, that your integration isn't complete, signaled by having two get_field('estate_sale_dates');, and the name $first_row being out of place when iterating all of them. Without the sort, you're basically doing
$repeater = get_field( 'estate_sale_dates' );
...
foreach ( $repeater => $row_that_isnt_used )
{
$rowses = get_field( 'estate_sale_dates' );
$first_row = $rowses[0];
}
I think something like this is what you're after:
<?php
while ( $latestes->have_posts() ) {
$latestes->the_post();
echo '<li class="frontpage-esale"><a href="';
the_permalink();
echo '">';
the_title();
echo '</a> ';
if ( ! count( $sale_dates = get_field('estate_sale_dates') ) )
echo '<span>Sale Dates TBA</span>';
else
{
// $order = array_column( $sale_dates, 'es-date' ): (php 5.5+)
foreach( $sale_dates as $ignore => $row )
$order[] = $row['es-date'];
array_multisort( $order, SORT_ASC, $sale_dates );
echo "<ul>";
foreach( $sale_dates as $row ) {
$date = $row['es-date'];
$start = $row['es-start-time'];
$end = $row['es-end-time'];
echo "<li>" . date('F j, Y',strtotime($row_date)) . " $start-$end</li>";
}
echo "</ul>";
}
echo "</li>";
}
I have the same problem with a calendar. There is no solution to do this in the right way with a repater field. The best way to do this, is to use an custom field in an hidden post type and a relation field.
Because any PHP solution is inperformant.
Make a Custom Post Type "Sales Dates" an in this a custom field "date" and a second "Post Relation".
Then select from Post Type "Sales Dates" all Posts order by meta key "dates" with the relation field you get the data from the not hidden post type.

Get authors post in wordpress with co-author plus installed

I want to list all posts, a user is assigned as author.
We are using the plugin, co-authors plus, which allows to assign multiple authors to a single post.
The function <?php $user_post_count = count_user_posts( $userid ); ?> returns the correct number of posts, the user is assigned to.
But when trying to list all the posts, with The loop only the post which initially were created by that user are shown.
query_posts( $args );
if (count_user_posts($user->ID) == 0) {
echo "No posts";
}
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
Is there an other possibility to get all posts from an user or can we modify our existing code?
EDIT
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
endwhile;
endif;
/**
* #param $user_id
* #param int $paged (if $paged = null return all posts)
* #return array|bool
*/
function get_coauthor_posts_by_author_id($id, $paged = 1)
{
global $wpdb;
$slug = 'cap-' . strtolower(get_userdata($id)->user_nicename);
$max_post = get_option('posts_per_page');
$post_lower_limit = 0;
if ($paged > 1) {
$post_upper_limit = $max_post * $paged;
$post_lower_limit = $post_upper_limit - $max_post;
}
$term_id = $wpdb->get_results($wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE slug = %s ", $slug))[0]->term_id;
$sql = "SELECT SQL_CALC_FOUND_ROWS $wpdb->posts.id FROM $wpdb->posts LEFT JOIN $wpdb->term_relationships AS tr1 ON ($wpdb->posts.id = tr1.object_id) LEFT JOIN $wpdb->term_taxonomy ON ( tr1.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id ) WHERE 1=1 AND (($wpdb->posts.post_author = %d OR ($wpdb->term_taxonomy.taxonomy = 'author' AND $wpdb->term_taxonomy.term_id = %d))) AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') GROUP BY $wpdb->posts.id ORDER BY $wpdb->posts.post_date DESC";
$sql_limit = " LIMIT %d, %d";
if ($paged !== null) {
$sql = $sql . $sql_limit;
}
$result = $wpdb->get_results($wpdb->prepare($sql, $id, $term_id, $post_lower_limit, $max_post), ARRAY_A);
return array_column($result, 'id');
}

PHP Function returns wrong date

I' am learning how to make a WordPress Plugin. I did make few simple plugins but not as complicated as this one. It's a Events Calendar. The var_dump from the function "nc_get_start_date()" on the page it outputs wrong dates.
The output from the var_dump(nc_get_start_date());
string(32) "1970-01-01,1970-01-01,1970-01-01"
This is what the function should return in real
23-12-2013, 25-12-2013, 26-12-2013
In the function.php on the plugin folder. This is the codes
/* Query to get the events post from the database */
function get_nc_events(){
global $post;
$query = new WP_Query(
array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => -1,
'order' => 'ASC'
)
);
return $query;
}
/* Get the start date from the above function */
function nc_get_start_date(){
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
When I write the code in page-caledar.php without the function it renders everything prefectly.
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
echo $wnc_start_date = $wnc_start_date[0] . "<br/>";
endwhile;
Problem Solved. Thanks everyone. The problem was in this function
/* Get the start date from the above function */
function nc_get_start_date(){
global $post;
$query = get_nc_events();
while ( $query->have_posts() ) : $query->the_post();
$nc_event_id = $post->ID;
$wnc_start_date = get_post_meta( $nc_event_id, 'wnc_start_date');
$wnc_start_date = $wnc_start_date[0];
$wnc_start_date = date("Y-m-d", strtotime($wnc_start_date));
$wnc_start_date_array .= "$wnc_start_date,";
endwhile;
return rtrim($wnc_start_date_array, ",");
}
I didn't make global $post;

WordPress count user posts of different custom post types

I've been looking for a way to count the number of custom posts a user has created, and was able to do it using this snippet:
<?php
$userid = get_current_user_id();
function count_user_posts_by_type($userid, $post_type = 'foo_type', $post_status = 'publish') {
global $wpdb;
$query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND post_type = '$post_type' AND post_status = '$post_status'";
$count = $wpdb->get_var($query);
return apply_filters('get_usernumposts', $count, $userid);
} ?>
And then echo the result with:
<?php echo count_user_posts_by_type($userid); ?>
My question: Above code outputs only the count of the custom post type "foo_type". If I have two custom post types - "foo_type" and "bar_type" - how do I change this code so that it returns the count of both of them rather than just the count of "foo_type"?
Add the second post_type to the query:
$query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = '$post_type' OR post_type='$post_type_2') AND post_status = '$post_status'";
Try custom function given below
function my_count_posts_by_user($post_author=null,$post_type=array(),$post_status=array()) {
global $wpdb;
if(empty($post_author))
return 0;
$post_status = (array) $post_status;
$post_type = (array) $post_type;
$sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND ", $post_author );
//Post status
if(!empty($post_status)){
$argtype = array_fill(0, count($post_status), '%s');
$where = "(post_status=".implode( " OR post_status=", $argtype).') AND ';
$sql .= $wpdb->prepare($where,$post_status);
}
//Post type
if(!empty($post_type)){
$argtype = array_fill(0, count($post_type), '%s');
$where = "(post_type=".implode( " OR post_type=", $argtype).') AND ';
$sql .= $wpdb->prepare($where,$post_type);
}
$sql .='1=1';
$count = $wpdb->get_var($sql);
return $count;
}
And then echo the result with:
<?php echo my_count_posts_by_user($userid , array('posttype1' , 'posttype2')); ?>
Please look on url given below..
https://wordpress.stackexchange.com/questions/43549/count-user-posts-by-user-id-post-type-and-post-status
thanks
$args = array(
'post_type'=> 'page',
'author' => '1',
'post_staus'=> 'publish',
'posts_per_page' => -1
);
echo custom_count_post_by_author($args);
function custom_count_post_by_author($args){
query_posts( $args );
$count = 0;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$count++;
endwhile;
endif;
wp_reset_query();
return $count;
}
You can pass any Arguments in $args which support query_posts

Categories