Is it possible to get the exact counter of each post for display in a theme I'm making? So for example if I have 100 posts, the last post will have a number of 100, the one before that: 99, and so on. I can't use the post's ID on this since this is always incremented by 2 (most of the time) making it highly inappropriate as a counter.
Try this, look at the results you will get the idea.
Don't forget the array starts with 0 not 1.
Each array item will contain the post ID. so if Item(key) nr 15 is the same as the current page ID, then the current item is #16(15+1)
<?php
global $wpdb;
$query = "SELECT `ID`
FROM `$wpdb->posts`
WHERE `post_status` = 'publish'
AND `post_type` = 'post'
ORDER BY `post_date` ASC
"
;
$count = $wpdb->get_col($query);
var_dump($count);
moar info: wpdb
Do look at the query if you agree with my order.
You'll have to use a "counter" there, something like this:
$i=1;
while(have_posts):
....
<h2><?php echo $i.' - ';?><?php the_title();?>
...
$i++;
endwhile;
Related
I have created a theme with a custom post type of reports. I installed a rating plugin that interacts with this post type, allowing users to rate the reports. It stores the post rating in two fields, sum and count where sum is the total for all ratings, and count is the number of individual ratings.
Example: If a 5 people rated a post as 1, 2, 3, 4, and 5, the sum would be 15 and the count would be 5.
When a user visits the reports archive page, they see a list of all posts of the report post type. However, I want to add a query parameter to filter down to posts with an average rating of 4 or higher. I'm currently trying to use the pre_get_posts hook as follows:
add_filter( 'pre_get_posts', 'filterReports' );
function filterReports( $query ) {
if( is_post_type_archive( 'reports' ) && $_GET['top'] ) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT *
FROM
wp_postmeta AS sum
wp_postmeta AS count
WHERE
sum.meta_key = 'sum' AND
count.meta_key = 'count' AND
sum.meta_value / count.meta_value >= 4"
);
}
}
I'm not entirely sure how to construct my custom query in the above. Any advice would be greatly appreciated.
Use below code will work as per your scenario.
add_filter( 'pre_get_posts', 'filterReports' );
function filterReports( $query ) {
if( is_post_type_archive( 'reports' ) && $_GET['top'] ) {
$reports_meta_query = $query->get('meta_query');
//Add our meta query to the original meta queries
$reports_meta_query[] = array(
'key'=>'count',
'value'=> 4,
'compare'=>'>=',
);
$query->set('meta_query',$reports_meta_query);
// somehow construct a query that checks if sum / count >= 4
}
}
Pretty sure the query you are looking for is something like this:
SELECT
sum.post_id,
sum.meta_value,
count.meta_value,
(sum.meta_value / count.meta_value) as result
FROM
wp_postmeta sum
LEFT JOIN wp_postmeta count USING(post_id)
WHERE
sum.meta_key = 'sum' AND
count.meta_key = 'count'
HAVING
result >= 4
You are basically joining twice the same table based on the post_id, so you can then query by the meta_key of both sum and count, then you look for the result of your math in a Having clause to check if the result would be bigger than 4 as requested.
Hope with this you can get what you were looking for.
Cheers
Hi all I am a new of wordpress,I want to create pagination next and previous, I have problame with pagination next and previous, I cant find the way to do it.I try to hard to do it, but still can not, How can I do it , Help me please. thanks you.
here is my code page_movie.php
<?php
global $wpdb;
$lastedate = $wpdb->get_var("
SELECT substr(meta_value,1,7) as Date
FROM ".$wpdb->prefix."postmeta
WHERE meta_key = 'details_release_date'
AND meta_value <> ''
AND meta_value >= '".date('Y-m-d')."'
GROUP BY substr(meta_value,1,7)
ORDER BY substr(meta_value,1,7) ASC
LIMIT 1
");
_e(do_shortcode('[movies limit="-1" type="Now Showing"]'));
_e(do_shortcode('[movies limit="-1" type="Coming Soon" compare="BETWEEN" start="'.date('Y-m-d').'" end="'.$lastedate.'-31"]'));
_e(do_shortcode('[movies limit="-1" type="Released" compare="BETWEEN" start="'.date('Y').'-01-01" end="'.date('Y-m-d').'"]'));
?>
<?php the_posts_pagination( $lastedate ); ?>
I have a custom table (ERA_Data) in my WP database, which I'd like to add pagination, column sort & search to.
Currently the following code successfully grabs the data (over 4000 entries) & displays them;
<?php
global $wpdb;
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
echo "<table>";
foreach($ERA_Data as $ERA_Data){
echo "<tr>";
echo "<td>".$ERA_Data->PartNo."</td>";
echo "<td>".$ERA_Data->Make."</td>";
echo "<td>".$ERA_Data->Carline."</td>";
echo "<td>".$ERA_Data->Model."</td>";
echo "<td>".$ERA_Data->Description."</td>";
echo "<td>".$ERA_Data->Start_Year."</td>";
echo "<td>".$ERA_Data->End_Year."</td>";
echo "</tr>";
}
echo "</table>";
?>
Just so you know, this is all new to me - so I'm happy to read up on further info, but if anyone can provide some code examples that may help - I'd be extremely appreciative.
With regards to pagination, I think 50 or 100 items per page would do.
I'd imagine sort order & search will be far more useful in any case.
I would juts work on this line for anything you need:
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data;");
so change it like this for showing only a 100, 50, etc. results
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 100;");
or
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data LIMIT 50;");
and for pagination, this link would help so much What is the best way to paginate results in SQL Server
for sorting, depending on your column, use this syntax
$ERA_Data = $wpdb->get_results("SELECT * FROM ERA_Data ORDER BY ***COLUMN NAME HERE***;");
Let me know if you have any questions!
Is there a way to display just ping counts (number) on Wordpress?
Actually there are comments_number function but that showing total count of comments, pingbacks and trackbacks.
The following code works on WordPress 2.9.1. It may work on other versions, but I only tested it against 2.9.1.
<?php
global $wpdb;
$post_id = get_the_ID();
$total_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback'");
$total_approved_pings = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1");
$post_ping_count = $wpdb->get_var("SELECT count(comment_id) FROM $wpdb->comments WHERE comment_type = 'pingback' and comment_approved = 1 and comment_post_id = $post_id");
echo "The total number of pings on this site is $total_ping_count.\n";
echo "The total number of approved pings on this site is $total_approved_pings.\n";
echo "The total number of approved pings on this post is $post_ping_count.\n";
?>
The above code gives counts just for pingbacks. If you want trackbacks instead of pingbacks simply change comment_type = 'pingback' to comment_type = 'trackback' or if you want a combined count change it to comment_type IN ('pingback', 'trackback').
Not exactly sure what you want: only show pingbacks? If so, and I haven't tried it, but Template Tags/wp list comments « WordPress Codex shows listing pingbacks and options.
I'm modifying the Recent-Changes WordPress plugin to display dates. I can echo the date but can't format it; e.g., mm/dd/yyyy.
I'd like the post_modified date to be in mm/dd/yyyy.
I've tried--
echo '<li>'.$RecentChange->post_modified('m/d/Y').
-- but that caused the plugin to stop displaying posts, and generally broke the site.
Below is the relevant chunk from the plugin--
/* define full SQL request */
$rc_sql = "SELECT post_date, post_modified, post_title, ID FROM wp_posts WHERE ".$rc_content_sql." ORDER BY post_modified DESC LIMIT ".$rc_number;
global $wpdb;
echo $before_widget;
echo $before_title.$rc_title.$after_title.'<ul>';
$RecentChanges = $wpdb->get_results($rc_sql);
if ($RecentChanges)
foreach ($RecentChanges as $RecentChange) :
$rc_url = get_page_link($RecentChange->ID);
echo '<li>'.$RecentChange->post_modified.' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
endforeach;
echo '</ul>'.$after_widget;
$wpdb->flush();
}
Try
<?php
mysql2date('m/d/Y', $RecentChange->post_modified);
?>
See reference.
Assuming RecentChanges->post_modified is a PHP date or time, you could wrap it in PHP's date function and format it how you want.
date("m/d/Y", $RecentChanges->post_modified);
So, your line would look like this:
echo '<li>'.date("m/d/Y", $RecentChanges->post_modified).' <a href='.$rc_url.'>'.$RecentChange->post_title.'</a></li>';
It's likely that your code is breaking WordPress because the post_modified function is just a getter and doesn't take parameters.