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 );
}
Related
I am using the Ultimate Member plugin and trying to trigger things after the registration form is filled in successfully.
As a test, I am simply creating a new post if the hook is run: um_registration_complete.
https://docs.ultimatemember.com/article/1234-umregistrationcomplete
function my_registration_complete( $user_id, $args ) {
// Create post object
$my_post = array(
'post_title' => 'function working',
'post_content' => 'hello world.',
'post_status' => 'publish',
'post_author' => 1,
);
// Insert the post into the database
wp_insert_post( $my_post );
}
add_action( 'um_registration_complete', 'my_registration_complete', 10, 2 );
Nothing happens after a successful registration. No post.
I tried adding die(); as a test to break the site on purpose if the hook runs, still nothing.
How best to debug this issue, I see nothing wrong with how I am using the hook but still it's not running.
Figured out my issue, and something I should have mentioned originally too. I am using the roots.io framework.
Functions etc namespaced, my add_action function name was not namespaced. Here is the working line:
add_action( 'um_registration_complete', __NAMESPACE__ . '\\my_registration_complete', 10, 2 );
I tried the following:
$insert = $wpdb->insert( $prefix."posts", array("post_title" => $posTitle,"post_content" => $postContent,"post_status" => "publish","post_type" =>"product"));
Using this query I Insert post when I check my post from admin and click on view is does not work when I update post from backend then it will work but I want to be view post without update it from admin mean afetr insert post with query post should work also use :
add_post_meta( $select[0]->productsId, '_wti_like_count', 0, true );
add_post_meta( $select[0]->productsId, '_wti_unlike_count', 0, true );
add_post_meta( $select[0]->productsId, '_wti_total_count', 0, true );
without any success. Can someone help solve this problem?
You shouldn't be using an insert for this purpose.
There's a function for exactly what you're trying to do.
wp_insert_post() - http://codex.wordpress.org/Function_Reference/wp_insert_post
$post_id = wp_insert_post( array(
'post_title' => $posTitle,
'post_content' => $postContent,
'post_type' => 'product',
'post_status' => 'publish',
) );
The post title variable is misspelled intentionally to match the example provided.
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...
These apis work perfectly.
http://domain.com/wp/api/get_tag_posts/?tag_slug=banana
http://domain.com/wp/api/get_category_posts/?category_slug=featured
Is there any way to join multiple tags or categories into a single request?
E.g.
http://domain.com/wp/api/get_category_posts/?category_slug=featured,news
or
http://domain.com/wp/api/get_category_posts/?category_slug=featured|news
I'm hoping to do what in SQL would be a "where category_name in ('featured','news')
You are after this
<?php $args = array(
'posts_per_page' => 10,
'category__not_in' => array(5,151)
);
query_posts($args);?>
This is what you need to do in PHP to obtain multiple posts from multiple categories. Now in case you are also looking for json or xml or any format it spits out. Put a new function in functions.php and register it with
add_action( 'wp_ajax_nopriv_getmyjson', 'myfunctionname' );
add_action( 'wp_ajax_getmyjson', 'myfunctionname' );
function myfunctionname()
{
Global $wpdb;
...
}
Call this in your theme or plugin and use action=getmyjson and url goes to admin_ajax with nonce set. After Global $wpdb you can use above function to bring all posts and then through them out as json object. Something like this
$response = json_encode( array(
'success' => true,
'message' => $ajaxmsg
'posts' => $mypostarray
)
);
// response output
header( "Content-Type: application/json" );
echo $response;
die(); //This would then make sure it is not getting anything else out of wordpress and sends only json out.
Once this is all done. You will have multiple posts out put in json format.
I needed to do the same thing and ended up using this plugin
Query Multiple Taxonomies. This plugin lets you do faceted search using multiple custom taxonomies. (author's description).
http://wordpress.org/extend/plugins/query-multiple-taxonomies/
It's got a handy sidebar widget also. Hope this helps!
Have you tried making the category_name an array? e.g.
<?php $args = array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news')
);
query_posts($args);?>
Also, with the tag, I found this example:
<?php query_posts('cat=32&tag=hs1+hs1&showposts=10'); ?>
or
<?php query_posts(array(
'posts_per_page' => 10,
'category_name' => array('featured', 'news'),
'tag_slug__and'=>array('banana')
)); ?>
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();
}