When someone click on Add New post then a message will appear like "you are not allowed to create posts" and users also not allow to publish or draft the post.
I tried this code -:
function post_published_limit( $ID, $post ) {
$max_posts = 5; // change this or set it as an option that you can retrieve.
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post'); // get author post count
if ( $count > $max_posts ) {
// count too high, let's set it to draft.
$post->post_status = 'draft';
wp_update_post( $post);
}
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );
This code helps me not to publish the post but it creates draft of that posts .
I don't quite understand what you mean but I think this will do the trick.
function post_published_limit( $ID, $post ) {
global $current_user;
get_currentuserinfo();
if ( user_can( $current_user, "administrator" ) ){
$max_posts = 5;
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post');
if ( $count > $max_posts ) {
$post->post_status = 'draft';
wp_update_post( $post);
}
}
}
Related
I'm trying to add the product title and some taxonomy terms to the image alt and image title text on the front end. I can add the product title with the function below but I'd like to append the taxonomy terms from the default woocommerce product_cat and also some custom taxonomies outlined below:
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes( $attr, $attachment ){
// Get post parent
$parent = get_post_field( 'post_parent', $attachment);
// Bail if it's not a product or if admin
$type = get_post_field( 'post_type', $parent);
if( is_admin() || $type != 'product' ){
return $attr;
}
// Get title
$title = get_post_field( 'post_title', $parent);
// Get Product Category
$product_cat = get_the_terms($post->ID,'product_cat');
// Get Artwork Category
$artwork_cat = get_the_terms($post->ID,'artwork-category');
// Get Artwork Materials
$artwork_material = get_the_terms($post->ID,'artwork-materials');
$attr['alt'] = $title;
$attr['title'] = $title;
return $attr;
}
Ideally, I'd like to output something like:
"$title, a $artwork_cat by $product_cat. $artwork_material" (E.g. The Mona Lisa, a painting by Leonardo Da Vinci. Oil on board.)
Any help would be great!
You have to pass the product id to the get_the_terms function which is you already getting in $parent. Try the below code.
function change_attachement_image_attributes( $attr, $attachment ){
// Get post parent
$parent = get_post_field( 'post_parent', $attachment);
// Bail if it's not a product or if admin
$type = get_post_field( 'post_type', $parent);
if( is_admin() || $type != 'product' ){
return $attr;
}
// Get title
$title = get_post_field( 'post_title', $parent);
// Get Product Category
$product_cat = get_the_terms($parent,'product_cat');
// Get Artwork Category
$artwork_cat = get_the_terms($parent,'artwork-category');
// Get Artwork Materials
$artwork_material = get_the_terms($parent,'artwork-materials');
$title = $title.', a'.$artwork_cat.' by '.$product_cat[0]->name.'. '.$artwork_material.'.';
$attr['alt'] = $title;
$attr['title'] = $title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2 );
I need to automatically create a custom post called 'project' when a logged in user purchased a product.
I put the following code on thank you page:
function get_last_order_id(){
global $wpdb;
$statuses = array_keys(wc_get_order_statuses());
$statuses = implode( "','", $statuses );
// Getting last Order ID (max value)
$results = $wpdb->get_col( "
SELECT MAX(ID) FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('$statuses')
" );
return reset($results);
}
$latest_order_id = get_last_order_id(); // Last order ID
//Auto creation Project post when user purchased
if (!count($latest_order_id)) {
return;
} else {
function programmatically_create_post_after_buying() {
$current_user = wp_get_current_user();
$current_user_id = $current_user->display_name;
$post_id = wp_insert_post(
array(
'author' => $current_user_id,
'post_title' => $current_user_id,
'post_status' => 'publish',
'post_type' => 'project'
)
);
}
add_filter('after_setup_theme', 'programmatically_create_post_after_buying' );
}
The code displays $latest_order_id correctly.
But, it doesn't create a post.
I can't find which parts are wrong, could you please help me?
I am a complete beginner, so there must be a lot of problems.
You can call 'woocommerce_thankyou' action from woocommerce thank you page. By using it you can get latest order id and create post based on that. You can add this code to your theme's functions.php
add_action( 'woocommerce_thankyou', 'create_post_on_order' );
function create_post_on_order( $order_id ){
global $wpdb;
//print('<pre>'.print_r( $wpdb, true ).'</pre>');
$order = wc_get_order( $order_id );
if( !$order->get_id() ){
return;
}
$current_user = wp_get_current_user();
$current_user_id = $current_user->display_name;
$post_title = $order_id.' - '.$current_user_id;
// Check post already exit with our title or not
$query = $wpdb->prepare(
'SELECT ID FROM ' . $wpdb->posts . '
WHERE post_title = %s
AND post_type = \'project\'',
$post_title
);
$wpdb->query( $query );
if ( !$wpdb->num_rows ) {
$post_id = wp_insert_post(
array(
'author' => $current_user_id,
'post_title' => $post_title,
'post_status' => 'publish',
'post_type' => 'project'
)
);
}
}
Note : You have to create post title unique so you can identify post based on order.
I'm developing an application through custom post type with custom fields using ACF plugin. When the custom post type is saved, it will create a blog in my network which works great however, when trying to copy the custom fields from the custom post type to the newly created blog's homepage with same acf custom fields, the acf custom fields are not saved.
I tested, $station_description = 'Not Empty'; and it does save/copied to its specific field without a problem. Its just that when i use, get_field() it does not save/copied to the new created blog.
What's wrong with my code?
function myy_acf_save_post( $post_id ) {
$post = get_post($post_id);
if( $post->post_type == 'network_directory' ) {
$title = $post->post_title;
$post_slug = $post->post_name;
$client_username = get_field('client_username', $post_id);
$station_server = get_field('station_server', $post_id);
$station_location = get_field('station_location', $post_id);
// $station_description = get_field('station_description', $post_id);
$station_description = 'Not Empty';
$language = get_field('language', $post_id);
$station_email = get_field('station_email', $post_id);
$station_website = get_field('station_website', $post_id);
$station_port = get_field('station_port', $post_id);
$station_logo = get_field('station_logo', $post_id);
// $station_logo_url = wp_get_attachment_url( $station_logo );
// $subdomain = get_field('subdomain', $post->ID);
$main_site = 'domain.com';
$subdomain_install = true;
# Create a new user
$check_user_id = get_user_id_from_string( $station_email );
if ($check_user_id !== null) {
$user_id = get_user_id_from_string( $station_email );
} else {
$rand_number = rand( 1, 2000 );
$username = 'user-' . $rand_number;
$password = wp_generate_password( 12, false );
$email = $station_email;
$user_id = wpmu_create_user( $username, $password, $email );
}
// wp_new_user_notification( $user_id, $password );
# Create site
if( $subdomain_install )
{
$newdomain = "{$post_slug}.$main_site";
$path = '/';
}
$the_blog_id = wpmu_create_blog( $newdomain, $path, $title, $user_id , array( 'public' => 1 ) );
if ( ! add_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id, true ) ) {
update_post_meta( $post_id, 'blog_id_meta_key', $the_blog_id );
}
switch_to_blog( $the_blog_id );
$homepage_id = get_option( 'page_on_front' );
// $station_logo_new_src = media_sideload_image($station_logo_url, $homepage_id, 'Station Logo','src');
// $station_logo_new_id = get_attachment_id_from_src($station_logo_new_src);
update_field('client_username', $client_username, $homepage_id);
update_field('station_server', $station_server, $homepage_id);
update_field('station_location', $station_location, $homepage_id);
update_field('language', $language, $homepage_id);
update_field('station_email', $station_email, $homepage_id);
update_field('station_website', $station_website, $homepage_id);
update_field('station_port', $station_port, $homepage_id);
// update_field('station_logo', $station_logo_new_id, $homepage_id);
update_field('station_description', $station_description, $homepage_id);
restore_current_blog();
}
}
add_action('acf/save_post', 'myy_acf_save_post', 9999);
throwing print_r(get_post_meta(get_the_ID())) in your template will output fields that are available, which can be useful debugging. In my case, after re-ordering modules on a page i noticed that get_post_meta output was unexpected, which then helped me see that i had forgotten to reset a query higher up the page using wp_reset_postdata().
fount it.
using get_post_meta to retrieve values is way better than get_field
EDIT: More Code.
Problem: I want to get the post meta of a post. It works fine for the case updated_post, but not for new_post and I just can't figure out why..
This is the function for the cases:
function userpro_sc_new_post( $new_status, $old_status, $post ) {
global $userpro_social;
$exclude = userpro_sc_get_option('excluded_post_types');
if ($exclude != ''){
$exclude_types = explode(',',$exclude);
} else {
$exclude_types = array('nav_menu_item');
}
if (!in_array($post->post_type, $exclude_types )) {
// new post
if ( $new_status == 'publish' && $old_status != 'publish' ) {
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'new_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
// updated post
if ($new_status == 'publish' && $old_status == 'publish' ){
$user = get_userdata($post->post_author);
$userpro_social->log_action( 'update_post', $user->ID, $post->ID, $post->post_title, $post->post_type );
}
}
}
And this is the code to run in the cases:
function log_action($action, $user_id, $var1=null, $var2=null, $var3=null) {
global $userpro, $userpro_social;
$activity = get_option('userpro_activity');
$timestamp = current_time('timestamp');
$status = '';
switch($action){
case 'new_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
case 'update_post':
$myId = get_post_meta(get_the_ID(), 'wpex_post_video_oembed', true);
$status .= $myId;
break;
}
Like I said, update_post works so I can see the ID... new_post does not work. Why?
I simplified the code to run a bit, but it is still the same issue.
Please help!
You have to be aware of three things before using get_post_meta() in your plugins.
You must declare global variables as global if any (eg: $wpdb).
You have to get the post data in $post_id (eg: $post_id = $_POST['postid'];).
Update the custom field value if needed (eg: update_post_meta($post_ID, 'video_id', true);).
Any of the above could be your problem. Please refer and try.
global $post;
$avriable_name=get_post_meta($post->ID, 'video_id', true);
Try the above code, global post will help to get the id for the post. If you didnt decalre it $post->ID will be blank and rest will not work.
Please let me know if you need further help.
Try this:
$myId = (get_post_meta(get_the_ID(), 'wpex_post_video_oembed',true));
I am using advanced custom field frond end posting.
<?php
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id;
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => 'A title, maybe a $_POST variable' ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// update $_POST['return']
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
?>
I want to use this method
<?php
$original_blog_id = get_current_blog_id(); // get current blog
$bids = array(1,2); // all the blog_id's to loop through
foreach($bids as $bid):
switch_to_blog($bid); //switched to blog with blog_id $bid
// ... your code for each blog ...
endforeach ;
switch_to_blog( $original_blog_id ); //switched back to current blog
?>
How can I Post to multiple blog at same time. please help me.
Problem Solved!. I got a solution from here.
http://support.advancedcustomfields.com/forums/topic/front-end-posting-to-multiple-blog/