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?
- How to Add Image to WordPress RSS-Feed with no Plug-in?
- Is it safe to use $_SERVER[‘REQUEST_URI’]?
- Namespacing WordPress project according to FIG standards
- How will php 7 affect WordPress?
- How to add active class to custom menu using while loop and wp_list_pages
- Get list of WP Updates Across Sites
- automated tests as a user?
- WordPress Wysiwyg Content not being displayed
- Want to create Child theme, but already edited Parent theme css files and some php files
- Switching between custom templates in a post type of the admin menu
- How to add a custom id to each post URL in wordpress
- How do I display the function the_content in this loop?
- Is there a way to get 3+ dimensional array from a single MySql command
- Admin Options page. Save as Array
- AJAX request status 200 but no actual “response”
- Delete a WordPress transient from a shortcode on Page/Post update
- How to enable Zend Optimiser+ with Batcache
- Getting the author name on author archive page
- Create own WordPress shortcode gallery
- Is sanitize_title_with_dashes formatting function too liberal (in terms of accepted characters)?
- How to prevent WP_Query function from returning all posts when empty?
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- dynamic dependent select dropdown
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- wp-comments-post.php file returns a blank page
- wp_mail file attachment not being placed in upload folder?
- Loading php file with AJAX in WordPress frontend
- Inline style to PHP [closed]
- How to get Post Type from the functions.php file
- Asynchronous multiple requests 401 Unauthorized
- Get the URL of the page from which an ajax request has been launched within ajax callback
- Edit the_category (); for a hierarchical display
- WordPress archive page showing 404 Error [duplicate]
- Division by zero error in image.php
- How to have different header.php based on the user logged in?
- How do I remove a category from a wordpress loop>
- How do I include SVG file used as featured image?
- Return product description
- How to add the sidebar to all the pages except the home page? [closed]
- Header not properly displaying on archive.php
- WordPress loop by meta key that is an array? and how loop multiple arrays
- Adding regular php file to site
- REST API works in browser and via AJAX but fails via cURL
- How to display different submenus?
- Remove slugs from custom posts type
- Simple jQuery AJAX Request fails to run PHP Script
- Update if post with specific title exists
- Pass important values server-side when processing form
- Wp_editor doesn’t send the required data
- pass html code to worpdress
- Can ‘Custom Field’ data be inserted into this Short Code?
- Merge 2 product attribute terms
- How do I reset $wp_query in a function?
- Add class to all img if they have a certain ACF field
- How can I check that the acf field value is not a duplicate when adding a post?
- Run do_shortcode on php template using JS function
- global $product is empty string when passed into function
- How to update custom fields when post is published?
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Trouble Accessing Gravity Forms API (GFAPI)
- Add a default style if no Featured image is selected
- When to use wp_register_script() function?
- setcookie() issues on running with HTTPS on WordPress
- How to load WP_Editor JS files manually (with AJAX)?
- Querying posts from wordpress with 2 or more conditionals
- Importing demo data in Multisite
- Posts with Custom Fields does not appear
- How to export database correctly for local to online
- Replace TinyMCE with Custom Built Editor
- Setting field not saving on front-end
- Getting custom field data from cart page to checkout page in woocomerce [closed]
- php grabbing every post made?
- Woocommerce Convert existing order to the cart
- Execute function when I click button
- Too few arguments – wp_login action
- Site Health and PHP 8.1
- “acf/load_field” Critical error upon saving/updating WooCommerce products [closed]
- Function extension
- How to improve the performance when using a flat-file dataset and PHP for implementing advanced search function?