Support » Fixing WordPress » function of -> operator

  • what is this “->” operator called

    and whats its function?

    <?php if($post->post_parent)
    $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
Viewing 15 replies - 1 through 15 (of 18 total)
  • Not sure what its proper name is (pointer?) but it allows you to reference a specific item within the returned data or object – $post in the above example.

    It’s an object operator for referencing an object’s properties(or some refer to them as instance variables).

    Referencing an object property.

    $object->property;

    Referencing an object method(an objects function)

    $object->method();

    Example

    class Foobar {
    	var $a = 'foo';
    	var $b = 'bar';
    	var $foobar;
    	function Foobar() {
    		$this->foobar = $this->a . $this->b;
    	}
    	function GetFoo() {
    		return $this->foobar;
    	}
    }
    $foo = new FooBar();
    
    echo $foo->foobar; // Echoes the object property/variable foobar
    echo '<br />';
    echo $foo->GetFoo(); // Echoes the object method GetFoo()

    When you refer to $post in WordPress you’re generally(unless you’ve coded it otherwise) get an object fed back to you, so when you’re referencing post data, you’re dealing with objects, hence the object operator ->.

    Hope that helps clarify things for you… 🙂

    Thread Starter delinquentme

    (@delinquentme)

    im lost on your example so i managed to find a simpler one…

    code1

    <?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?>

    SO this makes sense … now I’m caught up on programming terms

    is there a hierarchy of methods, objects, classes, functions and properties? (if there isn’t a simple answer don’t worry about it…real questions are below)

    these are some of my tests:

    code2

    <?php echo post_parent; ?>

    ^this returns “post_parent”

    code3

    <?php echo post_parent(); ?>

    ^this returns “Fatal error: Call to undefined function post_parent()”

    code4

    <?php echo the_ID(); ?>

    ^this returns “13”

    so in code4 “the_ID();” is a “function”

    now the “post_parent” ( in code1 ) is a “property” of the “object” “$post”

    THUS

    $object->property
    is akin to
    $post->post_parent

    and why i get a Fatal Error ( in code3 ) is because im trying to get a “property” of some object … but the object isnt specified

    correct?

    i THINK THINK THINK i might be learning something here…

    post_parent on it’s own is nothing (as is post_parent() – that’s just a standard function call, and no such function exists) .. it’s neither a property nor a method, since you’ve made no reference to an object.. (ie. using the operator ->, a hyphen followed by greater than, or right arrow if you prefer)

    $post is an object with various properties, post_title, post_parent, post_content, and so on… there’s various properties of data related to the given post.

    $post->func() would reference a method of the $post object, but since the $post object only holds data it does not have any methods(functions).

    NOTE: Objects can still be printed out in there entirety like with arrays, using print_r(), for dumping data onto the screen so you know what’s in any given variable..

    print '<pre>'; // This just formats the output so it's more readable
    print_r( $obj );
    print '</pre>';

    Some people prefer var_dumps, i personally just prefer this format.

    Thread Starter delinquentme

    (@delinquentme)

    WOW … so the first time i just got a chunk of data … but having it spaced out w the pre tags is AWESOME

    NOW… how do i access one part of that array?

    i looked through a few PHP tutorials and the it suggested a format of say

    $object['post_parent']

    for an associative array … but thats returning diddly…

    Arrays are somewhat different than objects..

    Example variable holding an array of data.

    $var = array(
    	'key1' => 'value1',
    	'key2' => 'value2',
    	'key3' => 'value3'
    );

    Example output.

    echo $var['key1']; // Output = value1
    echo $var['key2']; // Output = value2
    echo $var['key3']; // Output = value3

    http://php.net/manual/en/language.types.array.php

    Thread Starter delinquentme

    (@delinquentme)

    yeah that makes sense …. but check the output on this:

    Array
    (
        [64] => stdClass Object
            (
                [ID] => 64
                [post_author] => 1
                [post_date] => 2010-05-27 04:13:12
                [post_date_gmt] => 2010-05-27 04:13:12
                [post_content] => Go To Gals page
                [post_title] => GoToGal
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] =>
                [post_name] => gotogal
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2010-05-27 04:13:12
                [post_modified_gmt] => 2010-05-27 04:13:12
                [post_content_filtered] =>
                [post_parent] => 20
                [guid] => http://testing.black-glass.com/?page_id=64
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    
        [62] => stdClass Object
            (
                [ID] => 62
                [post_author] => 1
                [post_date] => 2010-05-27 04:12:53
                [post_date_gmt] => 2010-05-27 04:12:53
                [post_content] => East Coast Killers page
                [post_title] => EastCoastKillers
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] =>
                [post_name] => eastcoastkillers
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2010-05-27 04:12:53
                [post_modified_gmt] => 2010-05-27 04:12:53
                [post_content_filtered] =>
                [post_parent] => 20
                [guid] => http://testing.black-glass.com/?page_id=62
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )

    that looks like a nested, nested array. …

    so lets say i wanted the data from

    entry [64] at the top and i wanted to get the [post_title] (GoToGal)

    that would need the [64] as well as the [post_title] to be in the call … right?

    so how do those keys get placed inside the call ?

    Thread Starter delinquentme

    (@delinquentme)

    DIG IT

    this is how i did it:

    $children = get_children();
    
    echo $children[34]->post_title;

    where “34” is the index key to the “stdClass Object” (the format that the post_parent, post_title, post_content are arranged in) and “post_title” is any one of the “keys” for the array contained in “$children”

    now the only thing is theres gotta be a more direct way to do this…

    something like:

    echo get_children('34')->post_title;

    SNOWBALLING!

    more to come…

    Thread Starter delinquentme

    (@delinquentme)

    SO making progress …

    http://wordpress.org/support/topic/404854?replies=0#post-1533918

    these are the new issues ive run into on this little project

    IE how to access specific array-nested data

    Can you clarify what it is you’re trying to do?

    Exclude some pages from a page menu? (per your opening post).

    Thread Starter delinquentme

    (@delinquentme)

    basically its take the get_children() “method” (plz correct me if thats not what its called 😀 )

    then to cut out only those entries whos

    post_parent = $post->ID

    and a few things after that but that stuff simple and i think i can handle it 😀

    Thread Starter delinquentme

    (@delinquentme)

    the ISSUE is this:

    the get_children() returns an array w these crazy stdClass objects …

    so where a ref to a nested array would be bigArray[littleArray[key]]

    THIS kind requires a whole different kind of reference to get at the objects inside the stdClass object…

    ive found this…
    http://bytes.com/topic/php/answers/609821-accessing-stdclass-object-vars

    w the following code:

    $variables = get_object_vars($object);
    $keys = array_keys($variables);
    print $variables[$key[0]]->SysMessage;

    but something IM doing is incorrect

    I’m still not quite following what you’re aiming to do..

    get_children()

    ..is for getting children, so it will naturally return anything whose parent has the post’s ID, there’s no need for you to check if they have the post as a parent(they do), that’s what the function does.

    The returned data is an array of objects..

    Assuming you want to loop over this data, then all that’s left is creating a loop for it..

    ie.

    foreach( $my_array as $key => $object ) {
    
    	echo 'This key is ' . $key . '<br />';
    	print '<pre>';
    	print_r( $object ); // So you can see what's in the object for this key
    	print '</pre>';
    	// echo $object->property
    }

    I can only give you a general example because i’m still a little unclear on what it is you’re wanting to do.

    More on loops:

    While loops.
    http://www.w3schools.com/php/php_looping.asp
    http://php.net/manual/en/control-structures.while.php

    For loops.
    http://www.w3schools.com/php/php_looping_for.asp
    http://php.net/manual/en/control-structures.for.php

    Thread Starter delinquentme

    (@delinquentme)

    yeah get children returns what i need… what i was trying to do is automatically filter only those returned children objects PARENT = post id 20…

    ill use some terms to clarify 😀

    this code is on the “WEB” part of my portfolio … and the children ( as defined in the wordpress interface via setting the “parent page” are the individual client entries

    i can figure out the loop stuff but what i need to figure out is how to get the lowest level of object .. IE a post_parent or a post_title (as a string) from a stdClass object

    it just happens that ive got to nest the call to the above objects ( for the stdClass parameters) into an array call

    lol i HOPE this helps w the ambiguity a little?

    like i said i can figure out the loop stuff .. but links are appreciated … its the crazy nesting of the object inside the array which is runing my

    $testOutput = $children[$keys[i]]->post_parent;
    Thread Starter delinquentme

    (@delinquentme)

    u know what im beating around the bush here… i can make it a WHOLE lot simpler

    BAM!

    LIKE THIS:

    The print_r array dump:
    
    Array
    (
        [64] => stdClass Object
            (
                [ID] => 64
                [post_author] => 1
                [post_date] => 2010-05-27 04:13:12
                [post_date_gmt] => 2010-05-27 04:13:12
                [post_content] => Go To Gals page
                [post_title] => GoToGal
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] =>
                [post_name] => gotogal
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2010-05-27 04:13:12
                [post_modified_gmt] => 2010-05-27 04:13:12
                [post_content_filtered] =>
                [post_parent] => 20
                [guid] => http://testing.black-glass.com/?page_id=64
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    
        [62] => stdClass Object
            (
                [ID] => 62
                [post_author] => 1
                [post_date] => 2010-05-27 04:12:53
                [post_date_gmt] => 2010-05-27 04:12:53
                [post_content] => East Coast Killers page
                [post_title] => EastCoastKillers
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] =>
                [post_name] => eastcoastkillers
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2010-05-27 04:12:53
                [post_modified_gmt] => 2010-05-27 04:12:53
                [post_content_filtered] =>
                [post_parent] => 20
                [guid] => http://testing.black-glass.com/?page_id=62
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    
        [60] => stdClass Object
            (
                [ID] => 60
                [post_author] => 1
                [post_date] => 2010-05-27 04:11:51
                [post_date_gmt] => 2010-05-27 04:11:51
                [post_content] => National Signs page
                [post_title] => NationalSign
                [post_excerpt] =>
                [post_status] => publish
                [comment_status] => open
                [ping_status] => open
                [post_password] =>
                [post_name] => nationalsign
                [to_ping] =>
                [pinged] =>
                [post_modified] => 2010-05-27 04:11:51
                [post_modified_gmt] => 2010-05-27 04:11:51
                [post_content_filtered] =>
                [post_parent] => 20
                [guid] => http://testing.black-glass.com/?page_id=60
                [menu_order] => 0
                [post_type] => page
                [post_mime_type] =>
                [comment_count] => 0
                [filter] => raw
            )
    )

    what call would return me “GoToGal” from the stdClass Object ’64’

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘function of -> operator’ is closed to new replies.