Wordpress - Woocommerce wp_insert_post() 'post_category' - wordpress

I'm trying to import woocommerce products with wp_insert_post() function but i have problem with 'post_category'. My code is
$my_post = array(
'post_content' => $description,
'post_name' => $product_name,
'post_title' => $product_name,
'post_status' => 'publish',
'post_type' => 'product',
'post_author' => 1,
'post_category' => array(9,10)// ids from woocommerce categories
);
wp_insert_post( $my_post );
But categories are empty. I tried 'product_cat' => array(9,10) but nothing again. Can anyone help me?

Try using wp_set_object_terms
$post_id = wp_insert_post( $my_post );
wp_set_object_terms($post_id, 9, 'product_cat', true);
wp_set_object_terms($post_id, 10, 'product_cat', true);
Also ensure that terms with id 9 & 10 already exist in product_cat taxonomy before importing the product.

You should use slug instead id:
wp_set_object_terms($post_id, get_term(9)->slug, 'product_cat', true);
wp_set_object_terms($post_id, get_term(16)->slug, 'product_cat', true);
If you put here new category slug, WordPress will create it, and attached product to this category.

Related

Woocommerce - change number of displayed products in loop on archive pages

I want to change number of displayed products in loop on archive pages. I tried two snippets with no results:
// Change the Number of WooCommerce Products Displayed Per Page
add_filter( 'loop_shop_per_page', 'lw_loop_shop_per_page', 30 );
function lw_loop_shop_per_page( $products ) {
$products = 12;
return $products;
}
If the above snippet does not work for you, you can use the following
code snippet.
// Change the Number of WooCommerce Products Displayed Per Page
add_filter( 'loop_shop_per_page', create_function( '$products', 'return 12;' ), 30 );
My goal is to display 50 products per page.
You can do it with your own loop like
$products = new WP_Query([
'post_status' => 'publish',
'post_type' => 'product',
]);
if($products->have_posts()){
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template_part( 'content', 'product' );
}
}
it will show all products which are published also you can filter them by tax_query, and do pagination
$products = new WP_Query([
'post_status' => 'publish',
'post_type' => 'product',
'post_per_page' => 0,
'paged' => get_query_var( 'paged' ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => 'small',
);
),
]);
you can find everything about WP_Query here: https://developer.wordpress.org/reference/classes/wp_query/

How to add unfiltered_html capability

When posting html content below it posts fine with the admin, but any other user it strips all HTML tags
$my_post = array('post_title' => $title,
'post_content' => $content,
'post_category' => array(520),
'post_status' => 'Publish',
'post_name' => $url
);
$result = wp_insert_post( $my_post );
Is there a way to allow any user to post HTML tags.
I found that if I use this before the code, it works.
$sub_role = get_role( 'subscriber' );
$sub_role->add_cap( 'unfiltered_html', true );

Wordpress - Delete custom-post-type post

I am trying to add a button in wordpress that deletes a custom-post-type post with a specific title and current user as author.
Problem that occurring is that all the posts gets deleted, all the job_alert posts, not only for this specific author or with this title.
Can someone see why?
$delete_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$posts = new WP_Query( $delete_post );
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
wp_delete_post( get_the_ID());
}
}
I also have this code that creates a post and that works great. Similar code.
$new_post = array(
'post_type' => 'job_alert',
'post_title' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
$post_id = wp_insert_post( $new_post );
For deleting the particular post, here is the solution,
Please use name parameter instead of post_title in your query, then only it will return the required post which you want.
I have modified your code. Please find the updated code below:
$delete_post = array(
'post_type' => 'job_alert',
'name' => $title,
'post_status' => 'publish',
'post_author' => $current_user->ID
);
Now, your required post will be returned.
Hope, this may be helpful to you and let me know, if you have any query.
Thanks.

How to create post automatic after clicking chechout page in woocommerce?

whenever someone purchases the product test, a new post is created and saved under the category with the post meta being set with key "order_id" being set to the order_id of the order.
add_action( 'woocommerce_after_checkout_validation', 'test3' );
function test3(){
global $woocommerce;
print_r($woocommerce);
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test post',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
I think you want to create a new post with postmeta 'order_id' => 45 after checkout completion? if its so, my code will help you.
add_action('woocommerce_new_order', 'order_check', 10, 1);
function order_check($order_id) {
$my_post = array(
'post_title' => 'Test',
'post_content' => 'This is test post',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8, 39)
);
// Insert the post into the database
$post_id = wp_insert_post($my_post);
add_post_meta($post_id, 'order_id', $order_id);
}

Wordpress Plugin Add New Page

I have a created a wordpress plugin that needs to create a page dynamically.
When the plugin is activated a page is created COOKIE_POLICY.
From my reading I found that inserting into the DB is the best way. And below is the way to do it. However, when I activate the plugin, there is no page or post created.
I go this from:
http://codex.wordpress.org/Function_Reference/wp_insert_post
function create_policy() {
$my_post = array(
'post_title' => wp_strip_all_tags( $_POST['COOKIE_POLICY'] ),
'post_content' => $_POST['Here is all our cookie policy info'],
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,30 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );
This code is nearly correct. Below is the fixed code
function create_policy() {
$my_post = array(
'post_title' => 'cookiepolicy',
'post_content' => 'this is my content',
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 3,4 )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook( __FILE__, 'create_policy' );

Resources