Calling a specific row in single.php wordpress - php

I am trying to call a single row in each wordpress post. I was thinking that the best way to go around this is to insert a piece of code in the single.php file where I call via the post id:
<?php
$result = "SELECT post_date FROM wp_posts WHERE ID = get_the_ID()";
$row = mysql_fetch_assoc($result);
echo $row['post_date'];
?>
However this doesn't seem to work. Can anyone shed some light on why this isn't working?
Thanks!

WordPress offers a nice database abstraction layer to remove your need to read the posts table directly. It's apparent from your use of get_the_ID() that you're working with the "current" post or page in your code. So, guess what? That row of the posts table has already been loaded by WordPress. Try something like this to retrieve it.
$post = get_post();
echo $post->post_date;
See http://codex.wordpress.org/Function_Reference/get_post for more information.

The get_the_ID() in wordpress is a PHP function, not a SQL function. So try:
$result = "SELECT post_date FROM wp_posts WHERE ID = ".intval(get_the_ID());
*intval force the use of a number, and avoid the SQL injection.

<?php
global $post;
// $ids is an array with the required id's
$ids = array(1,2,24,234,2342);
$args = array(
'include' => $ids,
);
$myposts = get_posts($args);
//You always need $post, because setup_postdata assings eveyrhing to post...
//thats a Wordpres...way of things...
foreach($myposts as $post): setup_postdata($post):
the_time('F jS, Y');
//Don't forget to reset postdata
wp_reset_postdata();
endofreach;

Related

Unable to query using WP_Query with new database instance

I'm writing REST api in wordpress. The api is in OOP format. I created new instance of $wpdb and the connection works just fine. the only problem I'm unable to query database via WP_Query(). But using normal sql, I can get the result.
Whenever I query using WP_Query, it's seems like returning me, the table columns with null value.
FAILS
$args = array(
'post_type' => 'custom_type'
);
$result = new WP_Query($args);
SUCCESS
$query = "SELECT * FROM table_name WHERE post_type ='custom_type'";
$result = get_results($query);
I also tried adding slash before WP_Query like this to no avail:
$result = new \WP_Query($args);
Where's my mistake here? Someone please advise me. Thanks in advance.
You should call have_posts() function in wpquery object
$query2 = new WP_Query( $args2 );
if ( $query2->have_posts() ) {
// The 2nd Loop
while ( $query2->have_posts() ) {
$query2->the_post();
echo '<li>' . get_the_title( $query2->post->ID ) . '</li>';
}
// Restore original Post Data
wp_reset_postdata();
}
I kindly suggest you to use query_posts instead of WP_Query and reset postdata after query.
query_posts lets you to use pagination, excerption and other features.
After query_posts you're free to use a simple wploop

looking for an other way to get the last post ID from wpdb

I need to get the last post ID from $wpdb. For some reason $wpdb->insert_id only returns zero. What would be another way to get the last post ID?
The insert_id is a property, not a method. Try it without the () .
Wordpress specifies a get_the_ID() (Docs) function within "The Loop" which is typically used to iterate through posts outside of the WP site. In this case we just run through it and break after the most recent post ID (first ID in array) is retrieved.
<?php
require_once("path/to/your/wp-config.php");
$wp->init();
query_posts(array('post__in' => $postarray ));
while ( have_posts() ) : the_post();
$most_current_id = get_the_ID();
if ($most_current_id){
echo "Most recent Post ID: $current_id";
break;
}
endwhile;
?>
write the below sql query to get the last post id
$posts_table = $wpdb->prefix."posts";
$first_post = $wpdb->get_row("SELECT MAX(ID) FROM $posts_table WHERE post_status = 'publish'",ARRAY_A);
$post_table return the last post..

How to fix this WordPress function so that it doesn't return a 404 page?

I have the following function that I've added to my functions.php file in WordPress. The idea is that it gathers all of the titles of 'fsmodel' posts (a custom post type that I've created). It then returns these as an array, which I then use to populate a select tag in the custom meta fields for a second custom post type.
Basically, 'fsmodel' will have posts with a boat model, and the 'fsboat' post type will have a drop-down with the names of each of the models to select from.
Now, this appears to works fine in the Dashboard - the drop-down is populated as expected. When I save, however, the post doesn't show up in the Edit list. Also on the website, all pages output as the 404 error page when this function is active.
I'm certain that the problem lies within the following code - does anyone have any idea what I might have done wrong?
function fs_model_array() {
$models_array = array();
$loop = new WP_Query(array(
'post_type' => 'fsmodel',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_status' => 'publish'
));
while ( $loop->have_posts() ) : $loop->the_post();
$models_array[] = get_the_title();
endwhile;
return $models_array;
};
OK, I've come up with a solution (I hope - it's holding up for now).
Instead of creating a loop, I've just used the $wpdb->get_results to search the database for the column with a WHERE filter for the custom post type.
Then run an array builder:
$models_array = array();
$model_db = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_type='fsmodel' AND post_status = 'publish'");
foreach ($model_db as $model_db) {
$models_array[] = $model_db->post_title;
}
Thanks again for your time, hsatterwhite! :-)
I think you might find that adding wp_reset_query() to the end of your function will solve your problems :)
I like your solution, but I'd be inclined to say that you need to call the global variable of $post whenever you use the loop like this in a function, as it assigns it to that variable.
function fs_model_array(){
global $post;
$models_array = array();
$loop = new WP_Query(array(
...

How do I simply retrieve the last most recent publish post in wordpress with mysql?

I'm looking to retrieve the most recently published post in a wordpress system. I have some basic mysql understanding and I can find where the revisions are stored in the wp_posts table but what I can't seem to find is how to retrieve the permalink for the most recent post.
any ideas on how to retrieve the most recent post with mysql and permalink?
I see there are some existing functions from WP like so:
// get the latest blog entry
$myposts = get_posts('numberposts=1');
foreach($myposts as $post) :
echo '' . the_title() . '';
endforeach;
But when I put this on a custom page I'm working on, it seems to just be pulling out the page name I'm currently on and the link to this page (even though I'm thinking the above function should be retrieving a 'post'.
What am I missing?
Just in case you may want to have a real MySQL solution, here's what I use:
$query = "SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 1";
$post = mysql_fetch_assoc(mysql_query($query));
now the $post array holds all the data regarding to the latest post.
Worked out the solution:
<?php
global $post; // needed this
// get the latest blog entry
$myposts = get_posts('numberposts=1&orderby=date&order=DESC'); // and more stuff here
foreach($myposts as $post) :
?>
<?php the_title(); ?>
<?php endforeach; ?>

Get WordPress Post ID from Post title

I have an issue with a custom WordPress theme I'm developing. It's a bit convoluted, but essentially, what I need to do is get a Post Id by it's Post Title. In pseudo-code it would ideally be something like:
title = "foo";
post_id = get_post_id_where_title_is(title);
The title mentioned is a static reference not being pulled in from WordPress, it's already present on the page.
Just a quick note for anyone who stumbles across this:
get_page_by_title() can now handle any post type.
The $post_type parameter has been added in WP 3.0.
Found a solution if anyone else struggles with this. Only posted the question out of desperation after 4 hours testing/Googling!
function get_post_by_title($page_title, $output = OBJECT) {
global $wpdb;
$post = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type='post'", $page_title ));
if ( $post )
return get_post($post, $output);
return null;
}
Found at: http://sudarmuthu.com/blog/2009/09/18/retrieving-posts-and-pages-based-on-title-in-wordpress.html
Like Michal Mau mentioned:
Use
$my_post = get_page_by_title( 'My Title', OBJECT, 'post' );
echo $my_post->post_content;
It's ( $page_title, $output, $post_type ) to easily receive a post instead of a page.
May this will help you more by creating function so that you need not to repeat the code
function get_page_id_by_title($title)
{
$page = get_page_by_title($title);
return $page->ID;
}
$title = "your title";
get_page_id_by_title($title);
you can use the following code as per [a link][http://codex.wordpress.org/Function_Reference/get_page_by_title]1 )!
<?php
$page = get_page_by_title( 'About' );
wp_list_pages( 'exclude=' . $page->ID );
?>
Another way to get the post and page ID, is to use a plugin..
there is a plugin, that what it simply does, is just add a column to your all pages, all posts, all categories tables, and have a column title of ID...and right below, you will see all the page/post id listed in that column..
I think that should be very useful..
I use this plugin very frequently and it is very lightweight.
http://getyourblogready.com/?p=758
No need to use any type of SQL querys or plugin, use Wordpress standard functions for this
$page = get_page_by_title( 'Home' );
$page_id = $page->ID;
it is easy to get the post id from post title using wp query:
global $wpdb;
$rw = $wpdb->get_row( $wpdb->prepare("select * from "your post table name" where post_title='your variable name or your post title'"));
echo $rw->ID;
1) differ post_title and post_name from each other. post_name maybe is the slug. post_title is the title of post.
2)
$titlee = "yourtitle";
echo $id = $wpdb->get_var("SELECT ID FROM $GLOBALS['wpdb']->posts WHERE post_name = $titlee");

Categories