You can use the loop_start
hook which passes the $WP_Query
object by reference.
namespace StackExchange\WordPress;
class editFirstPostContent {
public function load() {
if( \is_admin() ) {
return;
}
\add_filter( 'loop_start', [ $this, 'loopStart' ] );
}
public function loopStart( \WP_Query $WP_Query) {
if( ! $WP_Query->is_main_query() ) {
return;
}
if( ! isset( $WP_Query->posts[0] ) ) {
return;
}
$WP_Query->posts[0]->post_content = sprintf(
'<div>Hello World!</div>%1$s',
$WP_Query->posts[0]->post_content
);
}
}
$editFirstPostContent = new editFirstPostContent();
\add_action( 'plugins_loaded', [ $editFirstPostContent, 'load' ] );
Related Posts:
- How to alter the query using pre_get_posts hook and is_post_type_archive
- How to use pre_get_posts
- Why is pre_get_posts hook invoked multiple times?
- Hooking get_pages()
- Is there a way to send HTML formatted emails with WordPress’ wp_mail() function?
- WP Cron Doesn’t Execute When Time Elapses
- Why do some hooks not work inside class context?
- Hook after image is uploaded and image sizes generated
- Do WordPress Core Filenames Work as Hooks?
- How to call a REST endpoint when a post is published?
- __NAMESPACE__ with register_activation_hook
- Hook for URL Request
- How do I flush the rules after saving settings using the Settings API?
- Hook/notify when any option or setting is added or updated
- Is it possible to save/modify a post without calling the “save_post” action hook?
- Which hooks is this? add_action(‘wp’,
- admin_notices after register_uninstall / deactivate_hook
- Rest API Hook When Post Is Requested
- add_action on inherit post status
- Why does wp_enqueue_script ignore my ‘wp_head’ hook?
- action lifecycle
- Redirect users on specific post category or category page
- Returning ACF custom field from publish_post
- How to modify an add_action() inside a loop of core function
- Add custom URLs to WordPress’s XML sitemap
- What is the best filter where to use register_block_type?
- How to send data from a form created by a plugin to an external API?
- How get list all users who edited post?
- Stuck in redirect loop after using wp_login action
- Hooks for Links Box [duplicate]
- Why doesn’t wp_enqueue_script() work when including a JavaScript file with TEMPLATEPATH?
- Check if do_action(‘custom_action’) is hooked into?
- Hook after wp_enqueue_scripts
- Detect change to user_meta and retrieve old and new values
- wp_login Action hook with conditional tag
- Can I remove WooCommerce main content hook?
- How to update WordPress core or themes and still have my child theme hooks work
- Proper indentation of code generated inside hooks
- Featured Image Action Hook
- Add parameter (time) to oembed
- How can I count post views of REST API calls and update them in an ACF field?
- Event-Driven Pattern vs MVC?
- Using hooks with extra parameters
- How to catch and modify custom field values when a page is updated
- Genesis: How to add content after aside and before the content-sidebar wrap
- new_to_publish fires multiple times
- Plugin init hook
- Best action hook for placing ical requests
- Is there a hook that fires when a row of wp_sitemeta table is updated?
- `rest_user_query` can’t access post author in post edit screen
- Post Meta Emtpy on Publish Using Transition
- How to check post type when using sanitize_title hook?
- How do I trigger a post update within a get_posts() foreach loop?
- Duplicate Cron Jobs Using wp_next_scheduled / wp_schedule_event
- Adding a hook to the default (page.php) template
- Hook when editing user
- get_post_metadata causing some meta data to fail
- How to change the default mail when admin approuved an user?
- How to modify how the_content outputs a link to an internal page
- read more, even if excerpt not trimmed
- How do I prevent term from being created on create_term hook?
- How to distinguish on hook profile_update user registering, user resetting password or user updating profile?
- How to add content to the end of a page with a hook?
- Hook function prints output twice
- Hook priority in admin with custom plugin
- Embeding style into the header via the function.php
- WordPress’s “Text” Format
- ‘save_post’ hook not working in WP 3.5
- Change the Default Plugin page filter to Active intead of All
- How to execute a hook asynchronously?
- Issues with if, else, and elseif statements
- Is there a before_save_post hook or something similar
- “Seen by” Feature in posts [closed]
- How to hook in to the “output buffer” and add html content
- hook for loading page
- What hook to use to redirect based on $post
- How can I modify the header of RSS feed items?
- Change status of page after an event (Looking for best practice advice)
- Print results of a WP_Query to wp-admin/upload.php within ‘delete_attachment’ hook?
- upgrader_process_complete filter not fired using ajax updates
- How to debug wordpress hooked functions?
- Elementor posts custom query based on tag [closed]
- remove_action(‘save_post’) in function that was triggered by save post not working
- How to get template name or category before publish
- WordPress How to reorder Page/Post Settings
- Is there a way to Insert a post to post list?
- “Intercept” enqueing of 3rd party’s JS file
- WP hook to add custom content after the_title()
- Can’t get ID of post that relates to the comment
- wp_get_current_user Not working
- Is there a hook that triggers when grant secondary user role in WordPress?
- How WordPress understands what do with the (all) key in the $wp_filter array? [duplicate]
- WP-Automatic to run publish hooks
- When is get_currentuserinfo() needed?
- Hide post completely and still reach it via cURL
- Problem in register activation hook and Copying folder
- Why does hook priority affects admin menu permission error?
- add_action failed to display function by a plugin
- Callback API for getting response from fygaro payment gateway
- Changing login url