wp_insert_post doesn't run plugins - wordpress

I made a wordpress plugin which posts on your behalf . I use wp_insert_post() function to do so , but it ignores the other plugins which are supposed to run when you add a new article. One of those is NextScript's snap plugin. Auto Post Thumbnail doesn't run also. Anyone had this problem before?

On success, wp_insert_post returns the post ID, which you can use to further process the newly inserted content, e.g. in the case of Auto Post Thumbnail:
$post_id = wp_insert_post( $args );
if( function_exists( 'apt_publish_post' ) )
apt_publish_post( $post_id );

Related

How to set post content's first image to be thumbnail image for more than 1000 records?

Good day, I'm having more than 1000 posts imported from the CSV files so most of them don't have featured images. I've try installing the plugins which help automatically updated the featured images based on the post content but they're not working in Wordpress 4.9.7.
Is there any solution or plugin that helps me to do this ?
Try to use plugins, so you can get the solution on same-
First Plugin - Sharing the plugin link, follow the link - WordPress Importer
Second Plugin - Sharing the plugin link, follow the link - Auto Upload Images
Hope!! it's useful for you..
I don't know if there is a plugin for that but here is the code if you want to do it yourself
you will need to collect the posts IDs in an array lets say "posts_ids"
$posts_ids = array();
foreach ( $posts_ids as $post_id ) {
$attachments = get_attached_media( 'image', $post_id );
if ( ! empty( $attachments ) ) {
set_post_thumbnail( $post_id , array_keys( $attachments )[0] );
}
}

Adding author info outside the loop in Genesis

I'm posting this question as I'm having a little trouble adding the author info to my post hero on my website.
I'm using the Genesis framework with Wordpress, so what I did is removing the post info from the post and adding it back in the post hero. That all works, except that the author name is not being shown anymore as it's not yet fetched in the post loop.
// Remove entry title
remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
// Remove post info
remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
// Add page title
add_action( 'hero-info', 'genesis_do_post_title' );
// Add page info
add_action( 'hero-info', 'genesis_post_info', 12 );
To be able to add the post author info back in the post hero, I've looked up stackoverflow and found a link where the OP was able to fix it by creating a shortcode for it and running it in the hero-info
function author_shortcode() {
global $post;
$author_id=$post->post_author;
the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');
This shortcode [author] is then added into
add_filter( 'genesis_post_info', 'custom_post_info' );
function custom_post_info( $post_info ) {
if ( is_archive() || is_home() ) {
$post_info = __( 'Article by [author] [post_author_posts_link] on [post_date] - [post_comments zero="Leave a Comment" one="1 Comment" more="% Comments" hide_if_off="disabled"]', 'tcguy' );
return $post_info;
}
}
This is the result now: http://imgur.com/a/6lX5J
It is shown in the wrong place for some reason. Anybody knows how this can be?
The site can be found here: http://websforlocals.com/business/
Hope I gave enough info, and that someone with the same problem can be helped out.
It is issue in your ShortCode registering php code.
When adding short-code we should not ECHO anything as this way it will not be echoed at the place we want to but at the top of the post content.
So always return the output in short code function, and then echo the short-code function.
Now WordPress have a convention for functions which echo the result and which returns the result, i.e. the_author_meta vs get_the_author_meta (first one which you are using will display/echo the result, however get_ functions will return the values).
We need to use get_the_author_meta instead of the_author_meta in your shortcode registering block and it will solve your displaying location issue.
function author_shortcode() {
global $post;
$author_id=$post->post_author;
return get_the_author_meta( 'display_name', $author_id );
}
add_shortcode('author', 'author_shortcode');

Wordpress ACF plugin: trouble getting field when saving post

Using ACF; I am making a plugin that hooks into WP on 'draft_to_publish' and on 'pending_to_publish' this part works fine. But when I try to get the ACF generated fields they return as blank, as if the fields generated by ACF haven't been set yet.
Short version of my plugin looks like this:
function scheduelMailChimp( $post ) {
// get post data and preb it for mail
$post_ID = get_the_ID();
$content_post = get_post( $post_ID );
$content = $content_post->post_content;
$postTitle = get_the_title( $post_ID );
//log debuginfo til debug.log
log_me(
array(
'get field date' => get_field($field_name, $post_id, $format_value)
)
);
}
add_action( 'draft_to_publish', 'scheduelMailChimp', 10, 1 );
add_action( 'pending_to_publish', 'scheduelMailChimp', 10, 1 );
The above code outputs empty. If I try to output something that is generated by WP and not ACF everything works like a charm.
all and any bright ideas are more then welcome :)
In case other people are looking for the same answer: After diving into a ton of Google searches, it seems that ACF haven't saved the data at the time draft_to_publish is fired. So I tried using the 'save_post' and it worked like a charm.

WordPress 4.0 - Display author's content on custom post type

Here my problem.
I have several users that are allowed to publish content.
I have create a custom post type to be used has a personal page.
I create a new custom post for each of the user.
They can publish media, events, posts... on this custom post, using tools provided by the theme Flawless.
The Flawless tools publish by default all the content, so if you want to add a gallery, it will show all gallery by all users. I want to limit the gallery display to only the current author of the custom post. And the same for all type of content a user may want to add to his page.
My understanding was that pre_get_posts was perfect in this kind of situation.
The goal was to publish on their custom post only their content, so I used the action pre_get_posts with this function:
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
$post_id = get_queried_object_id();
$author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = "$author_id";
}
}
add_action( 'pre_get_posts', 'only_current_author' );
This was working fine on my localhost with WordPress 3.9
Now I have updated to WordPress 4.0, and I got an error 404.
I don't understand exactly what cause the issue, and what is different between the two WordPress version, but I think this is what caused the error 404 is that I'm not able to get the author id of the current custom post.
So, I wonder, do you know alternative to define the author id inside the pre_get_posts action ?
Or should I use another method to filter what will be display?
I try several alternative, like adding an extra Query or adding the $query->query_vars in the template in my custom template, but none worked.
I try to get the author id using the name of the custom post found in the $query, nothing seems to work.
Thanks for your help.
So, after a good night of sleep, I return to my problems and found a solution.
Thanks to give me your feedback if you think this solution is appropriate.
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
global $wp_query;
$post_id = $wp_query->post->ID;
$post_type = $wp_query->post->post_type;
if( $post_id != 0 && $post_type == 'cpt_mdj' ) {
$post_author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = $post_author_id;
}
}
}
add_action( 'pre_get_posts', 'only_current_author' );
So, the issue was that $post_id = get_queried_object_id(); returned null, from that it was impossible to pre filter the query with the author id.
Using $wp_query, I was able to get the post id, and then the author id.
Side question, what is the difference between $query and $wp_query, they are identical when I var_dump, but give different result.
Thanks!

Wordpress get_post_meta does not return when using variable

I'm trying to get a plugin to read the post metadata of an attachment, and use it to update the content of the post it is attached to. I'm using the following in my themes function.php:
add_action( 'afip_created_post', 'get_desc', 10, 2 );
function get_desc( $post_id, $attachment_id ) {
$postmeta = get_post_meta ($attachment_id, '_wp_attachment_metadata', true);
$meta = $postmeta['image_meta'];
$mmwwdesc = $meta['description'];
wp_update_post( array( 'ID' => $post_id, 'post_content' => $mmwwdesc ) );
}
and then using this line in the plugin to hook the function
do_action( 'afip_created_post', $new_post_id, $post_id );
If I fill in the varibale "$attachment_id" with a number from another post i.e "15," I get the description inserted into the published post. If I change the output to wp_update_post( array( 'ID' => $post_id, 'post_content' => $attachment_id ) ); I get the id number echoed in the body of the published post. I'm don't understand why the original code does not work, since $attachment_id seems to be properly defined. I'm just a beginner with php. Is there something obvious that I'm doing wrong? Is the variable $attachment_id being echoed when it should be returned, or something technical like that?
Background: I'm using two wordpress plugins, one called Media Metadata Workflow Wizard (MMWW), and the other called Automatic Featured Image Post (AFIP). MMWW extracts the image metadata and writes it to the database as post meta. AFIP creates new posts using uploaded media, and attaches each image to a post, setting it as the post thumbnail. I don't think its an issue of the metadata not being ready when the function is called, because AFIP creates the post after the media has been uploaded, and had its meta written to the database. I'm also hooking my function as an update to the post, after it has already been created.
I've done extensive searching, and trial and error with no success. Can anybody help me out?
Thanks!
I think your issue here is that the get_post_meta function is returning blank in functions.php. The way to deal with this issue is to use a MYSQL query with wordpress to return the data in your psot meta.
First you need the global $wpdb object so at the start of your function write: global $wpdb;
Then you need to do the get_post_meta action using mysql and the $wpdb object:
$postmeta = $wpdb->get_var("SELECT meta_value FROM $wpdb->postmeta" WHERE meta_key = '_wp_attachment_metadata' AND post_id = $attachment_id");
Replace your get_post_meta line with the wpdb query and you should be fine.

Resources