WordPress.org

Forums

function of -> operator (19 posts)

  1. delinquentme
    Member
    Posted 4 years ago #

    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");
  2. esmi
    Forum Moderator
    Posted 4 years ago #

    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.

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    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... :)

  4. delinquentme
    Member
    Posted 4 years ago #

    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...

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    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.

  6. delinquentme
    Member
    Posted 4 years ago #

    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...

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    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

  8. delinquentme
    Member
    Posted 4 years ago #

    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 ?

  9. delinquentme
    Member
    Posted 4 years ago #

    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...

  10. delinquentme
    Member
    Posted 4 years ago #

    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

  11. Mark / t31os
    Moderator
    Posted 4 years ago #

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

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

  12. delinquentme
    Member
    Posted 4 years ago #

    basically its take the get_children() "method" (plz correct me if thats not what its called :D )

    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 :D

  13. delinquentme
    Member
    Posted 4 years ago #

    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

  14. Mark / t31os
    Moderator
    Posted 4 years ago #

    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

  15. delinquentme
    Member
    Posted 4 years ago #

    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 :D

    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;
  16. delinquentme
    Member
    Posted 4 years ago #

    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'

  17. Mark / t31os
    Moderator
    Posted 4 years ago #

    Well that depends how you want to reference it and what you want to use it for, but a direct reference would be...

    echo $your_var_holding_above_data[64]->post_title;

    Loop.. (print title for each post in the array)

    foreach( $your_var_holding_above_data as $post_id => $post_obj ) {
    	echo $post_obj->post_title . '<br />';
    }

    Does that help?

  18. delinquentme
    Member
    Posted 4 years ago #

    well it confirms that im not out of my mind hahah

    heres the code on the individual page im running ... w comments ... cuz my minds .. well ... out there hah

    the loop

    <?php
    	$children = get_children('post_parent=the_ID&depth=0');
    
    	// Get array keys for $children
    	$keys = array_keys($children);
    
    	// This returns good information
    	print $children[$keys['64']]->post_title;
    ?>

    and

    <?php
    
    	for ( $i=0; $i< count($keys); $i++ ){
    
    /* This should allow the incrementing (IE it goes up 1 each loop ... $i++ style) $i to step through the values in the array keys but also return the proper INDEX to allow the individual post to be selected since the indexes aren't sequential #s */
    		$index = $keys[$i];
    		// This returns good information
    		print "selector index: " . $index;
    		print "<br />";
    
    /* I've been expecting the below to return JUST the parent but instead i get something like this: "parent should be: 2013" where 20 is the parent ... and 13 is the $i value ...*/
    		$testOutput = $children[$index]->post_parent;
    		print "parent should be: " . $testOutput;
    
    	//if (post_parent = $post->ID){ THE IMAGES AND LINK STUFF }
    	}
    
    ?>

    so $index gets parsed correctly BECAUSE it returns the post_parent... but then the $i value is in there again somehow .. popping up in the value $testOutput

  19. delinquentme
    Member
    Posted 4 years ago #

    HHAAAAAHAHA

    print "<br />";

    so the parent of the next entry was simply butted up against it ...

    GOOD TO GO!

    Its so beautiful:

    The loop
    
    A child of this page is: gotogal
    A child of this page is: eastcoastkillers
    A child of this page is: nationalsign
    A child of this page is: vivaadventure
    A child of this page is: satiatours
    A child of this page is: testproject
    A child of this page is: evillizard

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.