Your first example with a separate variable is actually slower. In both cases you have the $post
variable already in your scope, and reading from that doesn’t cost anything. But if you create a copy and assign that to a new variable, you are using more resources. Just reading an existing variable doesn’t cost any time.
In real life, it doesn’t matter. The difference is so small that you probably can’t even see it in performance tests unless you do that with thousands of different variables at the same request.
What you should go for is readability. Using $post->ID
makes it a bit easier to see where that value is coming from than with $post_id
. In an IDE you can click on the $post
part of $post->ID
and get some information about this object, like the other variables and maybe available methods.
Related Posts:
- Nginx FastCGI_Cache Vs PHP Caching
- Conditionally loading JavaScript based on the Advanced Custom Fields in the post
- Extremely slow pageload for long post when using the_content?
- Performance issues with large website [closed]
- Why does running get_the_excerpt() when generating JSON file take 28 seconds vs. 599 milliseconds without it?
- PHP 5.5, w3 Total Cache, and opcache [closed]
- Using get_bloginfo(‘template_directory’) or variable – performance issue
- Looking for most performant way to execute several similar WP queries within shortcodes
- WordPress Script stops suddenly
- is there a benefit in using a constant over get_stylesheet_directory_uri?
- Page load timer?
- Can/should we delete wordpress cron jobs with no action?
- How do you measure the PHP generation time of your WordPress pages?
- Huge time to first byte on live site
- Serve cookie free URLs in WordPress without using a CDN
- (Xdebug Profiler) Why are there two cachegrind files generated for one page?
- Create page in templates for pages or the pages manager?
- Perfmon performance counters on WAMP
- WordPress article with 50 000 words loading slow – 100% cpu
- In WordPress how do you remove render-blocking CSS manually without a plugin?
- PHP XMLRPC for WordPress: Adding meta tags and description
- Blank on static home page?
- probleme adding Txt and Links in preg_match()
- Correct use of curly braces vs alternative synax
- Adjust the results quantity for Search Results page pagination
- Audio tags around Mp3 URL in content
- the_author_meta not working
- Missing Author Information
- Configuring WP-CLI on Windows 10
- Assign a picture URL to a page via PHP
- Check if Product is in a Specific Category in Functions.php
- Display user role Name – not slug
- Adding ads code between comments
- How to create and work with custom data / tables (i.e., for arbitrary data)?
- WooCommerce – Email admin with new user details
- Button not refreshing page
- Trying to add attribute to my posts’ featured image
- Transferring working local PHP site to wordpress – with database (MySQL)
- Using if/else statements with output from theme options
- How to implement a rating and review system for WordPress custom posts?
- Redirect specific user role to specific site after login?
- Send multiple attachments with wp_mail in PHP
- Ordering users by custom user meta
- php function to display commenter username or login
- custom css for admin only
- Odd / Even posts add class minus first post
- Sanitizing text fields in array
- Fatal Error in wp_get_current_user() Function
- Multiple meta_key in one global $wpdb;
- Parsing Menu Items and Blog Posts
- How to add div blocks after certain set of post
- Making my first wordpress template
- Contact Form 7 – Replace database configured form template with a static file
- HTML Special Characters in URL string [closed]
- Create page template via functions.php?
- How to pass argument to add_action while the method is inside a class?
- Getting taxonomy category Image from ACF [closed]
- How can I use a text and a function php
- Warning: call_user_func() expects parameter 1 to be a valid callback, function
- How to make changes to style.php [closed]
- Commas in Tag Cloud
- Adding an Anchor Link to a wordpress menu using WPBakery Page Builder
- WordPress SQL Prepare
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- WordPress loading progess – wp-blog-header.php
- How to hardcode header and footer in WP?
- Show a specific post in wordpress with links
- Search by date in WordPress
- What is wrong with this piece of PHP code? This is for an email template. (I am completely new to PHP)
- WP Website is shown in HTML
- Trying to add some custom text into WordPress Post title via function.php
- wp_update_post gives 500 internal error
- How can I remember an action taken by a logged in user?
- Script to generate pages – taxonomies not loaded
- cant insert data in a custom table in phpmyadmin
- Invalid Argument foreach
- get_template_part() not working with Advanced Custom Fields plugin
- List all ACF field values across every post on one page
- Closing Comments conditionally in comments.php
- wp_set_object_terms returns 500 error when called through AJAX function
- How do I send a POST request with params with WordPress REST API
- Create custom admin page with custom text fields
- Editable Student file associated with basic User ID
- html to posts, jpg to featured image, zip file contains jpg and html
- Two theme locations for two menus, but only one is showing up
- How to set the plugin load order when updating the active_plugins option?
- Display Year and Month from custom field + Age Calculator
- Shortcodes (with a space) added to php Sample
- No Authors for custom post type
- Sort posts by title, sort array by largest number [duplicate]
- Get author meta of all writers
- Creating a function that sanitizes the custom metabox excerpt
- Nav Menu Display
- WordPress & PHP sessions
- How to add pagination for wordpress home page
- which php or css file has generated an inline style in WordPress that overwrites my css
- Custom taxonomy with page post type – WordPress
- Alternative to php echo within code
- Why does WordPress not send the user the email to add a password?
- How to put a class in the p tag automatically added by the_content() in the wordpress loop?