for now i am making posts and products display as draft through it post and product will not display in frontend but is there a way to completely restirct user while he upload content.
add_action('init', 'make_editor_post_drafted');
function make_editor_post_drafted(){
$user = wp_get_current_user();
if ( in_array( 'editor', (array) $user->roles ) ) {
$user_ID = get_current_user_id();
$users_query = new WP_Query( array(
'post_type' => array('product','post'),
'meta_value' => $user_ID,
) );
if ( $users_query->have_posts() ):
while ( $users_query->have_posts() ): $users_query->the_post();
wp_update_post( array('ID' => get_the_id() , 'post_status' => 'draft' ));
endwhile;
endif;
} //end if
} //end function
Related
I am desperately trying to get back the attached images of a product in the Woocommerce publishing interface but it does not work.
My script works when I am in the editing of a post but not in the "editing of a product" part of Woocommerce.
I think it’s related to the fact that a product is a particular post, but I don’t see what to change to make it work.
I went through the creation of a plugin in order not to touch the file "functions.php" of my Divi theme.
My code :
global $pagenow;
if (( $pagenow == 'post.php' ) || (get_post_type() == 'post')) {
$postId = $_GET['post'];
$post = array();
$post = get_post( $postId );
$args = array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
$children = get_children( $args );
$images = array();
$images = apply_filters( 'get_attached_media', $children, $type, $post );
var_dump($images);
exit;
Thanks a lot !
I have a WooCommerce store for makeup, and we need to make it so that variation names are searchable, since people will often be looking for a specific shade name. I'm using this bit of code to make the product variations searchable:
add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );
function search_woocommerce_product_variations( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
}
}
Edited to add: I've already tried adding this in, with no changes in the search results:
$query->set('post_status', array('publish'));
The problem is that when searching on the name of a variation it's pulling up products that do that have that search term in the variation name, but are currently set to Draft status. How can I prevent that from happening?
Tell the query which post_status your looking for:
add_action( 'pre_get_posts', 'search_woocommerce_product_variations' );
function search_woocommerce_product_variations( $query ) {
if( ! is_admin() && is_search() && $query->is_main_query() ) {
$query->set( 'post_type', array( 'post', 'page', 'product', 'product_variation' ) );
$query->set('post_status', array('publish'));
}
}
<?php
$args = array(
'post_type' => 'product',
'numberposts' => -1,
);
$products = get_posts( $args );
foreach($products as $product):
$product_s = wc_get_product( $product->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
echo '</pre>';
}
endforeach;
?>
I have the following working code to create a new CPT entry when users register, but now I need this to happen only if the user has a subscriber role. This registration is processed by WooCommerce. I have another form thru Theme My Login for another users role.
$current_user = wp_get_current_user();
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$user_info = get_userdata($user_id);
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
if ($post_ID) {
// update ACF fields
update_field('field_5e0f8b80071a9', $user_info->first_name, $post_ID);
update_field('field_5e107dcfddc77', $user_info->user_email, $post_ID);
}
}
I have tried with
foreach( $user->roles as $role ) {
if ( $role === 'subscriber' ) {
// code here
}
}
and
if(in_array( 'subscriber', (array) $current_user->roles ) ) {
// code here
}
with no success.
How can I detect the user role and only if it is subscriber have the wp_insert_post ?
UPDATE:
the actual function, still not working:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$user_info = get_userdata($user_id);
$user_roles = $user_info->roles; // array with the user's roles
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) {
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
if ($post_ID) {
// inserisco campi ACF
update_field('field_5e0f8b80071a9', $user_info->first_name, $post_ID);
update_field('field_5e107dcfddc77', $user_info->user_email, $post_ID);
}
}
}
If I comment if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) it works fine
I finally solved it by using 'woocommerce_checkout_subscription_created' instead of 'user_register'
You are saving the user info inside of the variable $user_info. In your foreach you are using the variable $user and in your if clause you are using $current_user. I think this is the reason it is not working.
I would suggest saving the roles of a user inside of a variable and use the in_array function. Your code can look like this:
...
$user_info = get_userdata($user_id);
$user_roles = $user_info->roles; // array with the user's roles
// Here you can insert new post for registered users
$user_profile = array(
'post_title' => 'Profile '.$user_id,
'post_content' => '',
'post_status' => 'publish',
'post_author' => $user_id,
'post_type' => 'profiles'
);
if ( !empty( $user_roles ) && in_array( 'subscriber', $user_roles ) ) {
// Insert the post into the database
$post_ID = wp_insert_post($user_profile);
}
...
I have a custom post type 'product' that returns quite a lot of data in the API response, up to 400 posts with a lot of nodes. Almost all the data is coming from advanced custom fields (I'm using ACF to API plugin to expose it).
On the 'products' page, I only need to show the title & image of the product. Is there a way to remove all other fields when requesting all products with https://example.com/wp-json/wp/v2/product, but leave that data in place when requesting a specific product with https://example.com/wp-json/wp/v2/product/123 ?
You better create a custom endpoint for all products. Add the code below in your custom plugin or add it in functions.php of theme (I will recommend the custom plugin approach though)
You can then access it using https://example.com/wp-json/custom/v1/all-products
add_action( 'rest_api_init', 'rest_api_get_all_products' );
function rest_api_get_all_products() {
register_rest_route( 'custom/v1', '/all-products', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'rest_api_get_all_products_callback',
'args' => array(
'page' => array(
'sanitize_callback' => 'absint'
),
'posts_per_page' => array(
'sanitize_callback' => 'absint'
)
)
));
}
function rest_api_get_all_products_callback( $request ) {
$posts_data = array();
$paged = $request->get_param( 'page' );
$posts_per_page = $request->get_param( 'posts_per_page' );
$paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1;
$posts_per_page = ( isset( $posts_per_page ) || ! ( empty( $posts_per_page ) ) ) ? $posts_per_page : 10;
$query = new WP_Query( array(
'paged' => $paged,
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'post_type' => array( 'product' )
)
);
$posts = $query->posts;
if( empty( $posts ) ){
return new WP_Error( 'no_post_found', 'No Products Found.', array( 'status' => 404 ) );
}
foreach( $posts as $post ) {
$id = $post->ID;
$post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;
$posts_data[] = (object) array(
'id' => intval($id),
'title' => $post->post_title,
'featured_img' => $post_thumbnail
);
}
$response = rest_ensure_response( $posts_data );
return $response;
}
-I have successfully added a new tab to the Buddypress profile page and I also have my custom tab displaying my custom recipes template located in (buddypress/members/single/recipes.php)
function my_setup_nav() {
global $bp;
bp_core_new_nav_item( array(
'name' => __( 'My Recipes', 'buddypress' ),
'parent_slug' => $bp->profile->slug,
'slug' => 'recipes',
'position' => 30,
'screen_function' => 'my_item_one_template',
'default_subnav_slug' => 'recipes'
) );
}
add_action( 'bp_setup_nav', 'my_setup_nav' );
function my_item_one_template() {
add_action( 'bp_template_content', 'my_item_create_screen' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
}
function my_item_create_screen() {
locate_template( array( 'buddypress/members/single/recipes.php' ), true );
}
?>
Now I want to list all the users custom recipe posts in their profile page, so that other users can see what recipes the user they are currently viewing has created. This is my current code but it does not work properly. It just displays all recipes, not just the ones from that particular user.
<?php
$type = 'recipe';
$args = array (
'post_type' => $type,
'author' => $bp->displayed_user->id,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 10,
'ignore_sticky_posts'=> 1
);
$temp = $wp_query; // assign ordinal query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
else :
echo '<h2>Not Found</h2>';
get_search_form();
endif;
$wp_query = $temp;
?>
You're trying to use $bp without including it as a global.
But you don't need it.
Try changing
'author' => $bp->displayed_user->id,
to
'author' => bp_displayed_user_id(),