WordPress Post Title + suffix - php

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;

Related

Wordpress - Get tag id by slug

I am trying to get id\name of tag by it slug.
Thats my code:
$tag = get_term_by('slug', 'hedoms', 'post_tag');
$tag_id = $tag->term_id;
<h1><?php echo $tag->name;?></h1>
I took it from here:
https://codex.wordpress.org/Function_Reference/get_term_by
I have tag with the slug hedoms but the <h1> is empty, it not returns the value. I have tried this function (get_term_by) with category - and same result.
the 2nd field is the slug.
But if i do it with id and not slug, it works fine:
$tag_id = get_term_by('id', 97, 'post_tag');
echo $tag_id->name;
It look like the get_term_by recognize only 'id' as field.
What i miss here? it should be on loop or something ?
The file i trying to do it is archive-product.php of Woocommerce.
$tag = get_term_by('slug', ' hedoms','post_tag');
$tag_id = $tag->term_id;
OK i have found the solution.
I set on the get_term_by the taxonomy 'post_tag' instead 'product_tag' as it woocommerce tags. Next time i will look carefully on the url of the wp-admin when i am looking on taxonomy page:
...wp-admin/edit-tags.php?taxonomy=product_tag&post_type=product
So the code should be like that:
$tag = get_term_by('slug', $slug, 'product_tag');
$tag_name = $tag->name;
<h1><?php echo $tag_name; ?></h1>
Solved.

how to get page id in wordpress if Permalink is Post name

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' );

Echo Post ID from Post Slug or Post Title

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

trying to use query_posts('category_name=page_slug');

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 );

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