1

I'm trying to get the post title of a post_type (post/page/portfolio) in the admin panel outside of the loop.

I nearly tried all possible solutions, but without success:

//method 1
$content_post = get_post(3208);
$_menu_item_title = $content_post->post_title; //(Trying to get property of non-object error)

//method 2
$obj = get_post_type_object($_menu_item_type);
$content_post = get_post(3208);  //(Trying to get property of non-object error)
$_menu_item_title = $obj->$content_post->post_title;

//method 3
$_menu_item_title = the_title(3208); //(no error but nothing is store)

//method 4
$_menu_item_title = get_the_title(3208); //(no error but nothing is store)
  • Just making sure ;) If Method 1 returns a "non-object error", it normally means he can't find the post. – HU ist Sebastian Jul 17 '14 at 20:27
  • Method 2 and 3 will not work definitely. If you're sure that your post ID is 3208 then method 1, 4 should work. – obiPlabon Feb 13 '18 at 9:31
  • For method 1 var_dump($content_post);exit; after $content_post and check post get or not. If output is NULL then check post ID exits or not. – Jignesh Patel May 18 '18 at 4:35
1

As already been pointed out, the "non-object error" means get_post() returned nothing, and that the post does not exist.

If you are certain, though, that this post does exist, here is an alternative method taken from the codex

<?php
    $post_7 = get_post(7, ARRAY_A);
    $title = $post_7['post_title'];
?> 

If, again, nothing is returned, are you certain this post has been Published, and is not in a Draft status?

| improve this answer | |
  • Wouldn’t it be more efficient to use get_post_field? – Krzysiek Dróżdż Mar 17 '18 at 16:20
  • 1
    @KrzysiekDróżdż get_post_field() uses get_post(), so there's no difference in performance on the fetching side – stoopkid1 Apr 16 '18 at 21:07
1

If you have only ID of a post and you want only the title of that post, then using get_post_field will be best way to do this, I guess.

The syntax of this function:

get_post_field( $field, $post_id, $context );

So code, that will solve your problem looks like that:

$title = get_post_field( 'post_title', $POST_ID );
// most probably you want to display the title, so you can ignore last param

And addressing your code snippets... First method should work, if the post exists and it's published or current user can see it.

Second method can't work. It makes no sense.

Third and fourth methods can't work also. These functions don't take post_ID as param, so such use of them doesn't make much sense...

| improve this answer | |
-2

I did get the relevant title of the post using following process. first: I get information of post Id from dashboard [hover over the post and watch at the left bottom of the dashboard (you will get id information there)]

Second: inside wordpress loop I used:

<?php if(have_posts()): while(have_posts()) : $var_name= get_the_ID(); ?>
<?php if($var_name == post-id-value): get_the_title(); ?>
<?php endwhile; endif; ?>

here post-id-value is the integer value which is your post's id. You can store the get_the_title() value in a variable and echo function to echo out the value in your desired location. Note* you need to have at least one post in your wordpress dashboard. Otherwise you may get unexpected results ( browser even computer may crash)

| improve this answer | |
-3

using this default WP methods you can get the title of current page and current post.

<?php 
echo get_the_title();
| improve this answer | |
  • 1
    The author already pointed out that they tried using get_the_title(). Also, since they need it outside the loop, there is no current post. – swissspidy Nov 8 '17 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.