how post work after post_staus =>'publish' in query post? - wordpress

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.

Related

Wordpress wp_insert_post in post type doesnt work

I created a custom post type with CPT plugin. It's works fine and did the job.
I can create post, publish the post and i can see on the site.
But when i create a post from frontend via wp_insert_post it's creates the post but i can't see on site. It gives me 404.
$postId = "" . getUserCompanyId() . $menuId . "";
$m = array(
'insert_id' => intval($postId),
'post_title' => $menuName . ' | ' . $postId,
'post_name' => ''.$postId.'',
'post_content' => '',
'post_status' => 'draft',
'post_author' => get_current_user_id(),
'post_type' => 'm',
);
$insert = wp_insert_post( $m );
When i create a post:
When i publish and see on site:
Note: wp_insert_post works just fine when change post_type to post
insert_id is not allowed in post arguments array($m in your code) it must be ID.
But as you mentioned you need to create a post then ID is not required it is only needed when you wish to update an existing post.
Refer here for wp_insert_post(...)
Also, validate whether the post is created on not
$post_args = array('post_type' => 'your_custom_post',
/*other default parameters you want to set*/
);
$post_id = wp_insert_post($post_args);
if(!is_wp_error($post_id)){
//the post is valid
} else {
//there was an error in the post insertion,
echo $post_id->get_error_message();
}

I am getting the duplication issue wp_insert_post

Even viewing many answers here at StackOverflow did not solve the issue. I am using a page template and when I open that page the query runs but creates many duplicates. I am entering the 6000 simple records once it loads. I am also not logged in to the browser where I run this query. i have data in row[0] , row[1] ,row[2] .
Note: i am checking the post title with post_exists before inserting it but its still inserting the duplicates the original post url likes .com/my-post/ and duplicated record with , .com/my-post-2/
here is my code,
$postarr = array(
'post_status' => 'publish',
'post_title' => $row[0],
'post_content' => $row[0],
'post_author' => 1,
'post_type' => 'post' ,
'tags_input' => $cat_tags
);
$row_zero = htmlspecialchars_decode($row[0]);
if(!post_exists($row_zero)) {
$post_id = wp_insert_post( $postarr , $wp_error = false, $fire_after_hooks = false );
wp_set_object_terms($post_id, $row[2], 'writer');
}

Custom Post Type & Author not associating, user post count is 0, api doesn't return author in post objects

When Users create custom post types, the author field does not seem to be passed back to WP posts functions.
As the title says, I'm creating a user account programatically, and having that user log-in and write a post of custom post_type = 'job'.
This all LOOKS good in the database - the user, and post are inserted. The post_author is indeed the same as the ID of the user who created it.
But, in the User Accounts panel, that user post count is 0, and the API data object omits the author. I've configured the API for the additional custom post type and the endpoint works to return all post data EXCEPT author.
I've tried creating a post from the CMS with these users as well, and likewise, even if I give them Administrator permissions, they have a 0 post count - the posts are attributed to their author ID. I've also tried to force and update using wp_update_post();
Here's the code to create the user, and then the post:
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
//login
wp_clear_auth_cookie();
wp_set_current_user ( $user_id );
wp_set_auth_cookie ( $user_id );
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'subscriber' );
//Ready data for linked post type creation
$new_post = array(
'post_content' => $email_address,
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_id,
'post_title' => $post_name,
'post_name' => $post_name,
'post_type' => $user_type
);
//Create the post
$account_link_post_id = wp_insert_post($new_post);
There was an associated post to this one that shed light on this answer. When registering the post type, I was not setting enough fields in the 'supports' array of the register_post_type. Adding author, whatdayaknow, gives me the datapoint I'm looking for.
register_post_type(self::POST_TYPE,
array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'description' => __(""),
'supports' => array(
'title', 'author'
),
)
)
NOTE: Post count in the User Accounts page is still 0 - but, the API is returning the data I want/need.

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 to create/modfiy WP_Query to search in post title OR custom field?

I need to modify or create a WP_Query that will search for a search term in both the post title OR a custom field (called 'my_field').
I have been reading and trying for hours, but I am right back to this code (below) which, alas, only searches in 'my_field' and does not take the post_title into account.
function my_pre_get_posts_2( $query ) {
if ( is_admin() && $query->is_main_query() && $query->query['post_type'] === 'post' && isset($query->query['s']) ) {
$search_word = $query->query['s'];
$args = array(
//'s' => $search_word, //If I include this line, the WP query seems to AND post_title and my_field. If I comment out this line, the WP query only searches in my_field. (I need WP to OR post_title and my_field.)
'post_type' => 'post',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'my_field',
'value' => $search_word,
'compare' => 'IN'
),
array(
'key' => 'post_title',
'value' => $search_word,
'compare' => 'IN',
)
)
);
//$query = new WP_Query( $args ); //Need to modify the existing WP_Query
$query->init();
$query->parse_query($args);
}
}
add_action( 'pre_get_posts', 'my_pre_get_posts_2' );
The reason I need to do this is because I need to modify the behaviour of the the 'Search Posts' button in the 'All Posts' (admin) page so that whatever the admin user searches for, it will return the posts that have a matching post title OR my_field value.
To do an OR search, I tried merging two separate WP_Query results as shown here - https://wordpress.stackexchange.com/questions/55519/can-i-merge-2-new-wp-queryvariable-s - in guidod's answer. That wasn't a great solution though, and resulted in erratic behaviour.
The correct solution I found was to modify the query using the WP Custom Query as shown in the code (which requires some modifications) here - http://codex.wordpress.org/Custom_Queries .

Resources