wp_insert_post not working from within wp-admin - wordpress

I have a Wordpress problem I am trying to figure out; trying to create a wp-admin script that creates a new wp post…
Here is the code:
$my_post = array(
'post_title' => 'My post Gman99',
'post_content' => 'This is my post.',
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(8,39)
);
$post_id = wp_insert_post( $my_post );
echo $post_id;
And let me stress that this code works fine if I run it on a "front end" page; i just put the code in short code using "short code exec php" plugin and it creates the post no problem and spits out the post id; but when I try to do the same from within wp-admin it fails? dos anyone have any idea why this would be occurring?
here is the code of the php file that runs in wp-admin:
<?php
require_once('./admin.php');
if ( !current_user_can('edit_posts') )
wp_die(__('Cheatin’ uh?'));
require_once('./admin-header.php');
?>
<div class="wrap">
<br/>
<?php
$my_post = array(
'post_title' => 'My post Gman99',
'post_content' => 'This is my post.',
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(8,39)
);
$post_id = wp_insert_post( $my_post );
echo $post_id;
echo "<br/>";
echo "<h2>You have completed entering your New Slide.</h2>";
echo "<br/>";
?>
</div>
<?php
include('./admin-footer.php');
?>

I figured it out, my own mistake; but thanks so much to Rahil being willing to help…
basically the problem was that I was not calling the file that I thought; and so basically my code to insert the post was not being run when i thought it was; a really rookie mistake...

Related

After adding a post, no data is output acf

After the program has been programmed to add a post through the function
wp_insert_post()
Can not get data and fields in acf plugin.
The data is available inside the post, in the fields, but not on the client side.
The output is obtained only after clicking the "Update" button inside the administrative part. After that, the data is output
Filling the post is as follows:
$post_data = array(
'post_title' => wp_strip_all_tags($item->name),
'post_content' => "",
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_category' => array(3,13,2)
);
$post_id = wp_insert_post( $post_data );
update_field( "link_to_1", $item->image, $post_id );
update_field( "link_to_2", $item->tour, $post_id );
The conclusion on the client side is as follows:
<img src="<?php the_field('link_to_1');?>"/>
Assuming that link_to_1 is storing image url .
In acf field you have given the return value as image url.
<img src="<?php the_field('link_to_1');?>"/>

wp_query in add_action( 'admin_notices'

I have a custom post type - "data-result" of which there are 5 published posts.
I have another post type "data-collection-tool" that creates a "data-result" post from the front end when a user visits/edits a 'data-collection-tool' post and this works well.
I want to display a notice in the dashboard when the admin tries to edit a "data-collection-tool" post but where there a one or more "data-result" posts.
Code
function ws48356743_warn_questionnaire_editor() {
if(get_post_type() == 'data-collection-tool' ){
?>
<div class="notice notice-warning is-dismissible">
<p><?php _e( 'Done! '.wp_98435409_checkResultsExist(get_the_ID()), 'sample-text-domain' ); ?></p>
</div>
<?php
}
}
add_action( 'admin_notices', 'ws48356743_warn_questionnaire_editor',1,0 );
To do this I am querying "data-result" in a separate function:
function wp_98435409_checkResultsExist(){
$args_n = array(
'post_type ' => 'data-result',
'post_status' => 'publish',
'posts_per_page' => 9999
);
$p = get_posts($args_n);
print_r($p);
wp_reset_postdata();
}
get_posts returns an empty array and I can't figure out why.
All of the above code runs in functions.php
Does anyone have any pointers?
Thanks
A rogue space stopped this working
$args_n = array(
'post_type' => 'data-result', // <- was here in 'post_type '
'post_status' => 'publish',
'posts_per_page' => 9999
);

Issue on wpdb queries, wp_insert_post. The function adds duplicate post

Anyone encountered this issue?
I have created a custom php script which adds a new post by using wp_insert_post() but everytime I run the code it creates a 2 new post where in it should be only be one, its like it runs twice, so I tested another function using wpdb->query and its also the same its really weird. Please help I dont know what causes and how to fix this issue.
Thank you very much.
Here is a sample code i did just to test the issue and it still creates 2 identical post
function testtest(){
$ads_data = array(
'post_title' => "test",
'post_content' => "test",
'post_status' => 'publish',
'post_type' => 'ads',
//'post_author' => $user_id
);
// Insert the post into the database
$ads_id = wp_insert_post( $ads_data );
}add_shortcode('testtest','testtest');
SOLVED
I have figured it out now, whats causing the issue is on my header.php it is this line of code:
<link rel="icon" type="image/png" href="<?php echo esc_url( get_theme_mod( 'favicon' ) ); ?>" />
I find it really weird, i have no idea why that line of code is causing the issue, anyway thanks everyone!
Check before going to insert new post if already exists or not. So get_page_by_title('Some Post Title') == false then only insert new post.
add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title('test','OBJECT','ads') == NULL )
$my_post= array(
'post_title' => 'test',
'post_name' => 'test',
'post_type' => 'ads',
'post_status' => 'publish'
);
wp_insert_post( $my_post );
}

How do i show wordpress attachments from current post?

So with my blog i have a photo attachment page but it only shows to photo's at a time, and those two photo's are used as the navigation and i hate that.
I want the attachment page to show all the photo's that goes along with the rest of that set.
Here is the current code
<div id="nav-images" class="navigation clearfix">
<div class="nav-next"><?php next_image_link() ?></div>
<div class="nav-previous"><?php previous_image_link() ?></div>
How do i change that to show all the post attachments?
To clarify, this doesn't work anymore - at least with version 3.5.2. I used this instead;
$attachments = get_children(
array(
'post_type' => 'attachment',
'post_parent' => get_the_ID()
)
);
foreach ($attachments as $attachment) {
// ...
}
Only resurrecting an old thread because this one ranks quite highly for this search term.
When you're on a page or post, you can get all of its attachments with the following:
global $post; // refers to the post or parent being displayed
$attachements = query_posts(
array(
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->ID, // only get attachments for current post/page
'posts_per_page' => -1 // get all attachments
)
);
foreach($attachements as $attachment){
// Do something exceedingly fancy
}
Since you're currently on an attachment page, you can get all the other attachments using the $post->post_parent value:
global $post; // refers to the attachement object
$attachements = query_posts(
array (
'post_type' => 'attachment', // only get "attachment" type posts
'post_parent' => $post->post_parent, // attachments on the same page or post
'posts_per_page' => -1 // get all attachments
)
);
To then display the attachment images, you can use the wp_get_attachment_image_src function. The attachment's ID will be available in each iteration of your foreach loop as $attachement->ID (if you use the same naming convention as my first example).
Since WordPress 3.6.0 you can also use get_attached_media.
$media = get_attached_media( 'image', $post->ID );
if(! empty($media)){
foreach($media as $media_id => $media_file){
$thumbnail = wp_get_attachment_image_src ( $media_id, 'thumbnail' );
$full = wp_get_attachment_url( $media_id );
echo '<img src="'.$thumbnail[0].'" alt="'.$media_file->post_title.'" />';
}
}

Creating a post using PHP in one of the blogs in WP MU

I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.
If I use wp_insert_post() function, it's creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.
I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks
The following used as Must Use plugin does the job:
<?php
/* Plugin Name: New Post on Site Creation */
add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
{
switch_to_blog( $blog_id );
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1
);
wp_insert_post( $my_post );
restore_current_blog();
}

Resources