Including text at the end of every post

You can try the the_content filter, to append/prepend text to the post content. Here’s a simple example: add_filter( ‘the_content’, function( $content ) { if( is_single() && has_tag( ‘follow-me-on-twitter’ ) ) { $content .= ‘<div>Like my posts? Follow me on twitter!</div>’; } return $content; }, 99 ); where we append the extra text if the current … Read more

New posts defaulting to ‘0’ primary key

This is only my best guess. I can’t see a definitive answer with the information provided. The primary key for wp_posts should be set to ID. The primary key is giving the error. Since ID is not defined in the query, it is likely that the AUTO_INCREMENT value has been set to 0, or is … Read more

How can I extract the URL of a link from a post?

Exist some WordPress function that retrieve the list of links (in my specific case only one) that are into a post? Check out the core wp_extract_urls() function: $urls = wp_extract_urls( $content ); that will extract urls from any given content. Example $content=”Two test sites: <a href=”http://foo.tld”>Foo.tld</a> and https://bar.tld”; $urls = wp_extract_urls( $content ); print_r( $urls … Read more

Where is the “ancestors” post object attribute?

The reason you don’t see it if you dump object is because it’s not actual object property. WP_Post implements magical __get() method for ancestors and several more keys like that. When you access $post->ancestors() what you actually get is not some value from the object, but return of get_post_ancestors() function executed on it. So in … Read more

First hook to use current page post id

Answer by @Pieter Goosen : If you need to update the page before the main query fires and returns the page object, you will manually need to parse the URL (probably on init) and get the page ID from get_page_by_title() or get_page_by_path(). Otherwise, ‘wp’ would be earliest hook to get the page ID, for example: … Read more

How to share same post to multiple site in wordpress?

You can use the function switch_to_blog() for this $other_id = 1234 // the id of the other blog to save the post to switch_to_blog($other_id); $my_post = array( ‘post_title’ => $post_title, ‘post_content’ => $post_content, ‘post_status’ => ‘publish’, ‘post_author’ => $post_author, ); // Insert the post into the database wp_insert_post( $my_post ); restore_current_blog(); There is a pitfall … Read more

get_terms parent for current product only

get_terms relates to getting all the terms of a taxonomy. get_the_terms grabs all the terms related to the post. The problem is that it sounds like you only want to return those terms which are parent categories, not the children, and get_the_terms does not pass an arguments array. $terms = get_the_terms( get_the_ID(), ‘product_cat’ ); foreach … Read more