I try to get a post ID based on the post tile or slug (doesn't matter to me which one). After that I want to add the ID to a shortcode.
Working code
<?php
$test123 = get_post(30);
echo $test123->ID; /* this works and returns 30 */
?>
<?php
echo do_shortcode("[shortcode id='{$test123->ID}']"); /* this also works */
?>
So the next step is get the post ID based on slug or title. How do I do this? I tested different codes but nothing works till so far.
Many thanks for any help in advance!
To get post by slug use url to postid() function (documentation):
$post_id = url_to_postid( $url );
To get post by title you can use get_page_by_title() function (documentation):
$post = get_page_by_title( 'Your post title', OBJECT, 'post' );
$post_id = $post->ID;
Got the solution
<?php
$homepage1 = url_to_postid('/here-is-my-custom-post-permalink/post-slug/');
echo do_shortcode("[my_shortcode id='{$homepage1}']");
?>
If it is a normal post, you can skip the /here-is-my-custom-post-permalink/ part
Related
I need to able to insert the id of the post in the post title.
The id should be added to the title everywhere the title appears.
It should only be added to posts with post type post and not added to pages, custom post types etc.
I've managed to get this far:
function custom1_shortcode_func() {
global $post;
ob_start();
echo get_the_title($post->ID); echo " ("; echo get_the_ID(); echo ")"
$output = ob_get_clean();
return $output;
}
add_shortcode('post-id', 'custom1_shortcode_func');
Which returns the post title and the post id, when using [post-id] within a post.
But I need to have post titles modified all over my site, so wherever a post title is shown it is followed by "(post_id)".
I tried this and it did show the post_id before the post title, but it it changed all titles, including menus:
add_filter('the_title', 'wpshout_filter_example');
function wpshout_filter_example($title) {
return get_the_ID().$title;
}
You were close with your second attempt, but as you discovered that works for every title.
What you need to do is fist check the post type is a post and only add the id if it is.
add_filter('the_title', 'add_id_to_title', 10, 2);
function add_id_to_title($title, $post_id) {
//use the id to check the post type and only add the id if it has a type of "post"
if(get_post_type($post_id) == "post")
$title = $post_id.': '.$title;
return $title;
}
What this does:
The the_title filter can take 2 parameters: title (which you were using) and also the id of the post.
Using the id, can use the built-in get_post_type function to find out the post type.
Then we can and check if the post type is post and if it is we add the id to the title.
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 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;
I have looked everywhere and am not finding the answer I'm looking for.
Basically I want to access information from a post and display it on my front page.
I'm using a plugin called Advanced Custom Fields to organize my content for each post.
I can retrieve the post title like this.
$post_id = 148;
$queried_post = get_post($post_id);
?>
<h2><?php echo $queried_post->post_title; ?></h2>
<?php echo $queried_post->field_4f2af94aa1371; ?>
But I can not access the value of the custom fields. Can anyone help me?
Use the get_post_meta function
<?php echo get_post_meta($post_id, 'CUSTOM_FIELD_NAME', TRUE ); ?>
first param: post ID
second param: key (name of the meta value)
third param: true if you want a single value (instead of array)
For more information; http://codex.wordpress.org/Function_Reference/get_post_meta
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");