Like any other php object, you can add items to the $post object like so:
$post->my_new_val_name="my new value";
I don’t know exactly what you’re trying to do, but inside a function hooked to the_post, you can assign new values and return the object.
function my_func($post) {
$post->my_new_val_name="my new value";
return $post;
}
add_action( 'the_post', 'my_func' );
However, in your template file, you won’t be able to just echo $my_new_val_name as you’re suggesting… the the_post() function doesn’t extract values that way. You’ll have to reference the post object explicitly. Like:
echo $post->my_new_val_name;
Related Posts:
- How to store the_title() into a variable to reutrn the value, not just echo it
- Cleanest/Fastest way to avoid calling and retrieving data from the database multiple times?
- How do I know what variables are passed in a filter/action and what their meaning is?
- Count the number of times the search form template gets included on a page
- How to put a variable in a instance in the widget
- Get value from shortcode to do something
- Creating WordPress Shortcode with Variable
- Changing the order of custom fields in the dashboard for Woocommerce variable products [closed]
- How to reference PHP in Javascript
- Overide Variable in Child Theme
- How to send a php var to jQuery Ajax call with two different file
- Custom global variable not working in function
- Best way to use variables in multiple functions?
- Trying to add some custom text into WordPress Post title via function.php
- How do I fix Undefined variable using $_POST in function?
- Pulling a variable into the wp_nav_menu function
- Where can I find the declaration of `$_wp_theme_features`?
- Mega Menu Walker
- How to add a class to the attachment images
- How to call a function only once (global variable scope)
- Multiple is_page() in page.php
- Pass a PHP variable to another file
- How to preserve PHP modifications while upgrading WordPress?
- Inserting Gravity Form checkbox values into Advanced Custom Fields [closed]
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Shortcode created to check language not works
- Best way of adding CSS which can be manipulated by the user via theme option panels?
- How to Append to the_excerpt() Function
- Listing all sub-pages?
- Why do filters/actions require an argument count?
- How can I default to all WordPress roles when parameter is not included in shortcode?
- Recent post display using shortcode
- Check if term object is in array
- Highlight Current Tag in wp_tag_cloud
- Getting rid of role=”navigation” in the Home Page Pagination
- syntax issue on php 7.4
- How can I use wp_insert_comment to write a comment when a post is edited?
- How to remove the excerpt in the Dzonia Lite theme
- How do you add customer capability after Woocommerce purchase?
- Get first URL from post content
- Setting custom canonical urls
- Using a variable in is_page(array())
- Trigger popup in a php if/else statement
- Menu — How to add “current-menu-grand-ancestor” css class
- functions.php is being included twice, creating PHP fatal errors
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- How to output a PHP file values by shortcode?
- get a simple array of all of the term names that exist in all taxonomies
- Logged in user ID as post ID
- Show categories then when clicked on a category list all sub category and when clicked show all posts in that sub category
- How to store data from multiple forms using ajax and php
- How to control WordPress image metadata (using Imagick)?
- Removing “wpautop” (auto tags) only on certain pages?
- 3 Slashes appear after Apostrophe in custom fields after updating product-site
- Trying to retrieve post meta
- Need help enqueueing webfonts
- Error call_user_func_array() expects parameter 1 to be a valid callback when using image_size_names_choose
- Run a code only on theme activation only during first activation
- Shortcode from a function not working
- Is it possible to return content, and then also continue to do other things?
- Add meta tags to a custom header
- Need php code’s output to display underneath a Divi Module
- WP_Query | IF within $args array | Help me only add specific arguments if the argument is not blank
- How to use mysql LIKE with wpdb?
- woocommerce related product title length
- javascript variable to wordpress php variable
- Function sanitize_title() does not appear to be working
- Query for user roles
- Use SCSSPHP to compile Theme Customizer Values into .SCSS files ready to compile to CSS
- Displaying Event within two given time frames
- How to prevent a function from running based on host (ie web vs local)?
- How to call custom function outside from the admin page?
- Is there a way to create a “copy post” link?
- Use of antispambot with $curauth->email
- Delete images from media library when user deletes an image from ACF Gallery
- Reject all malicious URL requests functions.php
- Notice: Undefined variable
- How to override WooCommerce Order Item Meta with data from Cart?
- Taxonomy Child Term, Counter is staying on 0
- Access to “My Site” is missing from the admin bar
- How to redirect (301) trashed post to it’s parent taxonomy TERM ARCHIVE instead of 404 page
- How to call WordPress PHP file using external js file
- Need help with PHP functions
- Using get_theme_mod with checkbox to display content
- WordPress styling recent post
- Styling admin page rows in order of importance (checkboxes)
- Insert wordpress tags below posts via functions.php
- Break a WordPress function to run in patches and re-continue
- Pagenav Not appearing on custom Template
- My title is showing after the shortcode
- Load a Header in wordpress
- Trouble figuring out how to get my button to submit comment
- Adding multiple conditional tags in a function?
- How do I change the Go To Categories link in the term_updated_messages
- WooCommerce – Show ‘Sales’ badge on variable products where only some variations have sales price [closed]
- Add multiple HTML attributes to an Elementor button
- Automatically change insurance quantity based on cart total [closed]
- /page/2/ redirect to 404
- How to pass value to add_filter wpcf7_form_tag from another function?
- hiddenQuestions.find is not a function