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");
Related
I'm trying to display a list of the people who have authored AND edited a page in Wordpress, not just the people who have authored revisions. the_modified_author() gives me the name of the person who last saved the page. birgire suggested this code to get the list of all editors, but it seems to only show the authors of the original post and all revisions, and doesn't include the names of those who saved a change that wasn't actually a revision:
function get_the_modified_authors_wpse_99226(){
global $wpdb;
$authors = array();
$results = $wpdb->get_results( $wpdb->prepare("SELECT post_author FROM $wpdb->posts WHERE (post_type = '%s' AND ID = %d) OR (post_type = 'revision' AND post_parent = %d) GROUP BY post_author", get_post_type( get_the_ID() ), get_the_ID(), get_the_ID() ) );
foreach($results as $row){
$authors[] = get_the_author_meta('display_name', $row->post_author );
}
return implode(", ", $authors);
}
Sometimes people edit a page (for example, they make an edit to a custom field) and then update the post. When that happens, they would be listed under the_modified_author() but would not be considered an author of a revision.
What I need is a combined list of both revision authors AND those people who have edited (saved) the page without creating a revision. Sort of like the function for modified_author but without the '_edit_last', true part.
Is this possible?
Please try the below code :
$revisions = $wpdb->get_results("select * from {$wpdb->posts} where post_parent={$post_id} and post_type='revision'")
You may need to do some change according to your need.
i want the page id of current page in wordpress . i know get_the_ID() which is used to get the page id when Permalink Settings is Default . But in my case Permalink Settings is Post name and i want the page_id is it possible ? . if yes then how ?
Try This:
<?php
global $post;
echo "pageid: ".$post->ID;
?>
You can try this is you have page name
function get_page_id($page_name){
global $wpdb;
$page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
return $page_name;
}
get_the_ID() gives you the current ID of a post/page in a loop. Wordpress loads the page or the post in a loop that's why when you run get_the_ID() it gaves you the ID. Now that function has nothing to do with the permalinks. If you're not in any loop (for example you're trying to run that when wordpress is being initialized, you won't get the ID you're looking for because that part is not already set.
get_the_ID() works anywhere no matter what the permalink structure is. The only case i noticed it gives you a result other than the current page or post is when you're already in a loop other that wordpress default's. In that case, get_the_ID() will return the ID of the current post ID in that loop. You can learn more about the loops in the codex.
Now if you're still lost, can you provide a sample of code where you're using that function and you don't get the result you're expecting?
you can use get_page_by_title() if you really want ..
Full parameters are
get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' );
so use like this
$custom_post = get_page_by_title( 'pagename', OBJECT, 'your_custom_post_type' );
$post = get_page_by_title( 'pagename', OBJECT, 'post' );
and just to complete the answer, there is also the similar
get_page_by_path()
$page = get_page_by_path( 'about/us' );
I´m trying to query posts after category name and want the ('category_name=cat-name'); cat-name to be taken from the page title, lets say the page title is "Forename Lastname" then the perma-link will be /forename-lastname/ . Is it possible to make the cat-name copy the "forname-lastname" with php or must I try to make some a javascript to take care of it?
The "permalink" of the page it's stored as its post_name, so you can get it from the global $post
For instance:
global $post;
$slug = $post->post_name;
$posts_in_cat = query_posts('category_name='. $slug );
I have been looking for hours and I cant seem to find a solution, so was hoping some one here can help.
In wordpress when you have a post with the same title you get a number at the end of the post title for the url (/test, /test-2, /test-3). When I use the function get_the_title(); it only returns "test". I need a function that will return the post title and the -number.
Is there such function or is there a work around?
Thank you.
What you're looking for isn't the title, it's the slug. It's found in the $post object.
<?php
global $post;
$slug = $post->post_name;
$duplicate_number = array_pop( explode( '-', $slug ) );
?>
http://ideone.com/OS5bjA
You can save two posts with same post title. The number that adding to the post title was not actually title its postname. If you want to title with number then this will help you
global $post;
echo $post->post_name;
or
$sql = "select * from wp_posts where post_title='test';
$post = $wpdb->get_row($sql);
echo $post->post_name;
What php code can be used to retrieve the current page ID in a Wordpress theme? Then pull the booking_type_id from the database that is linked to that ID.
I have tried so many different things and I just cant seem to get it to work.
I am trying to use something along the lines of
<?php
$bookingid = $wpdb->get_var("SELECT booking_type_id FROM hobookingtypes WHERE post_id = 'the_ID();' ");
// Echo the booking resource id
echo $bookingid;
?>
But I am really not sure what I am doing wrong.
You can't use the_ID() because its echo the POST_ID, so use the global object $post the provide all the information related to post;
global $post;
$bookingid = $wpdb->get_var(
"SELECT booking_type_id FROM hobookingtypes
WHERE
post_id = '". $post->ID ."'"
);
// Echo the booking resource id
echo $bookingid;
Alternative of global $post:
OR you can use get_the_ID() instead of the_ID()
You can get current post ID with this code,
global $post;
$id = $post->ID;
or just,
global $post_id;
$id = $post_id;
or you can use,
get_the_ID() function insted.