0

I have below the code and I have to get the post id using the post title.

        $atts['speakersname']='abc,xyz,prq'; // post title 
        $getspeakertitle=explode( ',', $atts['speakersname']);
        $speakerpostid[]='';
        foreach ($getspeakertitle as $key => $value) {
         $speakerpostid[] = get_page_by_title( $value, OBJECT, 'speaker' );
        } 
       echo"<pre>";
       print_r($speakerpostid); 
       print_r($speakerpostid->ID);
       print_r($speakerpostid['ID']);
       print_r($speakerpostid[0]['ID']); 

I have tried this and I am getting an error

print_r($speakerpostid->ID);

Notice: Trying to get property 'ID' of non-object in

print_r($speakerpostid['ID']);

Notice: Undefined index: ID in

Tried below code also it's displaying first id value

  print_r($speakerpostid[0]['ID']);

I have to pass the ID to the below code

$s_post = get_posts(array(
          'showposts' => 10, 
          'post_type' => 'speaker',
          'post_status'  => 'publish',
          'post__in'  => array('34'), // I have to pass id here to get all the details
            )
           );

Any idea what i am doing wrong here

user9437856
  • 2,360
  • 2
  • 33
  • 92
  • `$speakerpostid[]='';` is not how you instatiate an array. You are not only declaring the array with your code, you are also pushing a blank string as the first element -- which of course does not have an ID property or element. This is fundamentally an off-topic typo question, but you need to educate yourself on the very basics of how to create an array and how to access its data. – mickmackusa Nov 13 '22 at 08:52
  • @mickmackusa, Yes, I have tried the array $speakerpostid=array(); – user9437856 Nov 13 '22 at 09:30

1 Answers1

0

Try this:

You are getting the whole post object in the $speakerpostid array. You need to store only the post id.

$atts['speakersname'] = 'abc,xyz,prq'; // post title
$getspeakertitle = explode(',', $atts['speakersname']);
$speakerpostid = array();
foreach ($getspeakertitle as $value) {
    $page_obj = get_page_by_title($value, OBJECT, 'speaker');
    $speakerpostid[] = $page_obj->ID;
}
echo "<pre>";
print_r($speakerpostid);
Krunal Bhimajiyani
  • 1,115
  • 3
  • 12