How to alter Wordpress global post title using php - php

I know i can use:
global $wp_query;
$wp_query->is_page = true;
For example to set the global is_page conditional to true.
Can i change the global post title in a similar way so it would effect the the_title() returned value?
To clarify things:
I need for a use of a "virtual page" case, where no post is actually loaded and i don't want to use any existing post title. just inject some custom title to the current globals so i will get it when using the_title on the current page.

To modify the title you can use a build in hook of wordpress:
function suppress_if_blurb( $title, $id = null ) {
if ( in_category(' blurb', $id ) ) {
return '';
}
return $title;
}
add_filter( 'the_title', 'suppress_if_blurb', 10, 2 );
https://codex.wordpress.org/Plugin_API/Filter_Reference/the_title

Finally found it. gladly it's very simple :) but this piece of code will also create all other post vars for a "fake post/page":
$post_id = -99; // negative ID, to avoid clash with a valid post
$post = new stdClass();
$post->ID = $post_id;
$post->post_title = 'Some title or other';
$wp_post = new WP_Post( $post );
wp_cache_add( $post_id, $wp_post, 'posts' );
global $wp, $wp_query;
$wp_query->post = $wp_post;
$wp_query->posts = array( $wp_post );
$wp_query->queried_object = $wp_post;
$wp_query->queried_object_id = $post_id;
$wp_query->is_404=false;
$wp_query->is_page=true;
$GLOBALS['wp_query'] = $wp_query;
$wp->register_globals();
More details here!

Related

Wordpress getting single post ID

I'm using a theme template and when i try to get post ID it returns the ID of the template not the ID of the actual single post.
the template ID is: 215
the post ID is: 1911
the following code will only output 215
function metavalue() {
global $post;
$meta = get_post_meta($post->ID, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');
get_the_ID(); the_id(); $post->ID; will also output 215. i need a way to get the actual single post ID so i can get the custom field value from 'product_url'.
I've contacted support with the theme authors on this topic as well but for the time being i've found a way to work around it.
function metavalue() {
global $wp;
$url = home_url( $wp->request );
$correct_post_id = url_to_postid( $url );
$meta = get_post_meta($correct_post_id, 'product_url', true);
return $meta;
}
add_shortcode('url_short', 'metavalue');

Appending Post Meta to Beginning of Post Content

I'm trying to alter the behavior of the Publish to Apple New Wordpress plugin. My theme uses a custom field for video embeds but the plugin doesn't recognize that content. I'm trying to append the meta to the beginning of the posts in Apple News. This is my code that isn't working:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return .$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
If I for instance alter the code to the following:
function add_post_meta_content($content) {
$meta = get_post_meta( get_the_ID(), 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content);
apply_filters( 'apple_news_exporter_content_pre', $post->post_content, $post->ID );
It appends "Print this content before the post" to the beginning of the post without issue. What am I missing here?
Plugin is already sending $post->ID to your filter, so no need to call get_the_ID().
Try this code:
function add_post_meta_content($content, $post_id) {
$meta = get_post_meta( $post_id, 'csco_post_embed', true );
return 'Print this content before the post'.$meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);
--
If that doesn't work, make sure that you actually have something saved in the database meta table under csco_post_embed key. Easy way to confirm this is to open your database and do a quick query:
SELECT *
FROM wp_postmeta
WHERE post_id = ENTER_YOUR_POST_ID_HERE AND meta_key = 'csco_post_embed';
Awesome! You got me 99% of the way there. Had to return the oembed to get the video to work. Here's the final code in case someone else comes looking.
function add_post_meta_content($content, $post_id) {
$meta = wp_oembed_get( get_post_meta( $post_id, 'csco_post_embed', true ) );
return $meta.$content;
}
add_filter('apple_news_exporter_content_pre', add_post_meta_content, 10, 2);

WordPress get current post id of custom post type in functions.php function

I have a function in my functions.php file and I need the current post ID.
I have tried getting it like this:
global $wp_query;
$currentID = $wp_query->post->ID;
echo '<pre>';
print_r($currentID);
echo '</pre>';
but doesn't seem to work since it says:
Trying to get property of non-object
EDIT: Entire function in functions.php
add_filter( 'gform_pre_render_1', 'populate_posts' );
add_filter( 'gform_pre_validation_1', 'populate_posts' );
add_filter( 'gform_pre_submission_filter_1', 'populate_posts' );
add_filter( 'gform_admin_pre_render_1', 'populate_posts' );
function populate_posts( $form ) {
global $wp_query;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos($field['cssClass'], 'booking-option') === false ) {
continue;
}
$currentID = $wp_query->post->ID;
var_dump($wp_query->post);
$choices[] = array( 'text' => $price, 'value' => $price );
$field->placeholder = '0';
$field->choices = $choices;
}
return $form;
}
Anyone can help me out please
Thanks a lot!
In your function add
global $post;
echo $post->ID;
To have access to the post id you must be within the loop
Otherwise , you must modify your function to accept the post id as a parameter , and hook where is safe to get post id, like so:
add_action('template_redirect', function() {
if (is_single())
your_function(get_queried_object_id());
}
});
function your_function($id){
//Do what you want
}
Some references:
https://wordpress.stackexchange.com/questions/177262/cant-get-post-id-in-functions-php?rq=1
https://wordpress.stackexchange.com/questions/140753/get-current-post-id-in-functions-php
As a note, for the future i think is more appropriate if you post these questions to the http://wordpress.stackexchange.com community
EDIT:
now that you posted the entire code i see that you are using gravity forms (which you didn't mentioned before).
This is a completely different question then.
You must obtain the post_id from the Entry object that gravity forms will pass to your function
https://www.gravityhelp.com/documentation/article/entry-object/
if you want to print the ID then use
the_ID();
if you want to store it then use
$postId = get_the_ID();
Use it in the loop
Hope this helps
Take Care and Happy coding
Getting the current post ID
The ID can be stored as a variable using
<?php $postid = get_the_ID(); ?>
To print
<?php echo $postid; ?>
By function
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
For Reference : click here

Wordpress Word Count to exclude shortcodes?

I have a function that gets the overall word count within a post.
function ic_word_count() {
global $post;
$ic_content = strip_tags( $post->post_content );
$ic_stripped = strip_shortcodes($ic_content);
return $ic_stripped;
}
I'm trying to get it to exclude all shortcodes, so basically exclude anything with square brackets and everything in between them. For example exclude [shortcode] or [This Shortcode]
Any idea on how I could add that part to the above function?
WordPress has a handy function, strip_shortcodes. Codex details here: https://codex.wordpress.org/Function_Reference/strip_shortcodes
Your function uses $post->ID but you aren't getting the global post value.
Finally you already have access to the post content inside the global $post object so just use that instead of get_post_field.
E.g. global $post;
function ic_word_count() {
global $post;
$wc_content = $post->post_content;
$ic_word_count = str_word_count( strip_tags( strip_shortcodes( $wc_content ) ) );
return $ic_word_count;
}
A smaller version:
function ic_word_count() {
global $post;
return str_word_count( strip_tags( strip_shortcodes( $post->post_content ) ) );
}

How can I get some of a posts meta data whilst it is still inserting?

I'm trying to make a function where a child post of a certain post type inherits the same title and slug as its parent. I'm using WP Types to define my post types and their relationships. But I'm having trouble with the below code:
function copy_parent_post_title( $post_id ) {
$new_post = get_post($post_id);
if($new_post->post_type == 'carnews-adverts') {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
$parent_title = get_the_title($parent_id);
$post_slug = sanitize_title_with_dashes($parent_title);
$post_update = array(
'ID' => $post_id,
'post_title' => $parent_title,
'post_name' => $post_slug
);
remove_action( 'wp_insert_post', 'copy_parent_post_title' );
wp_update_post( $post_update );
add_action( 'wp_insert_post', 'copy_parent_post_title' );
}
}
add_action( 'wp_insert_post', 'copy_parent_post_title' );
The problem is this line:
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
I presume it is because at this point the post's meta data hasn't been inserted into the database yet? If so how can I achieve what I want by accessing the get_post_meta upon inserting a post?
Thanks
I think to access the '_wpcf_belongs_carnews_id' that is being added by WP Types, you need to look in the $_POST array. However, WP Types might call add_post_meta() after calling wp_insert_post(). In this case the meta data won't be present if you hook into wp_insert_post.
Instead, hook your function into add_post_meta:
function copy_parent_post_title( $post_id, $meta_key, $meta_value ) {
$new_post = get_post($post_id);
if (($new_post->post_type == 'carnews-adverts') &&
($meta_key == '_wpcf_belongs_carnews_id')) {
$parent_id = $meta_value;
$parent_title = get_the_title($parent_id);
// ... Rest of your function
}
}
add_action( 'add_post_meta', 'copy_parent_post_title', 10, 3 );
This may be completely wrong as I barely ever use Wordpress.

Categories