I have created a custom post type and I have Woo Subscriptions. I would like to create a function that detects the post author role of each post.
If the author role is "Unsuscribed" then, change post status by that author to Draft. So with this, if users stops to pay the subscription, automatically their posts will not be shown.
Thank you!
I think that you can use the add_user_role action hook. This hook fires after a user has been given a new role.
add_action( 'add_user_role', 'unpublish_non_subscriber_posts', 10, 2 );
function unpublish_non_subscriber_posts( $user_id, $role ) {
if ( $role !== 'unsubscribed' ) {
return;
}
$user_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'author' => $user_id,
'fields' => 'ids'
) );
if ( count( $user_posts ) === 0 ) {
return;
}
foreach ( $user_posts as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft'
) );
}
}
Related
Spent 3 days trying to get the WP_Query to work with Elementor, however had no luck so far. Elementor either refuses to process the query or does not filter the posts at all. Can somebody help me and point out some mistakes that I have in my code?
Context: the query looks for current WP user's enrollment records of post_type 'tutor_enrolled' in 'wpv1_posts' and collects IDs of user's enrolled courses into $course_ids[]. The query should then filter only the posts with IDs matching the $course_ids array, therefore displaying all of current user's courses on Elementor frontend.
function custom_query_callback( $query ) {
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
$args = array(
'post_type' => 'courses',
'post__in' => $course_ids,
'posts_per_page' => -1,
'orderby' => 'post__in'
);
$query = new WP_Query ( $args );
}
}
}
add_action( 'elementor/query/custom_query_callback', 'custom_query_callback' );
I tried calling get_course_ids() and custom_query_callback() as two separate functions and trying to pass the $course_ids array in $query->set( 'post__in', $course_ids );.
Then I also moved the variables outside of the function, ending up with:
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
}
add_action( 'elementor/query/custom_query_callback', function( $query ) {
$query->set( 'post__in', $course_ids );
});
None of the above resolved my issue and kept causing errors in Elementor.
I need to validate if a custom field called family_id exists. If this family_id exists or is already created I need to send a message that it is duplicated and that a new one should be assigned.
NOTE: That id is not the one that wordpress assigns automatically is a custom field of ACF.
This is the custom field image
custom field family_id
And this is the code I have:
add_action( 'rest_api_init', function() {
/*INSERT SINGLE family*/
register_rest_route( 'api/v2','insert_family', [
'methods' => 'POST',
'callback' => 'insert_family'
] );
} );
function insert_family( $data ){
$parameters = $data->get_params();
$family = array(
'post_title' => sanitize_text_field( $parameters['post_title'] ),
'post_status' => 'publish',
'post_type' => 'family',
'meta_input' => array(
'featured_family' => sanitize_text_field( $parameters['featured_family'] ),
'family_id' => sanitize_text_field( $parameters['family_id'] ),
)
);
$family_id_created = wp_insert_post( $family );
if ( is_wp_error( $family_id_id_created ) ) {
wp_die( $family_id_created->get_error_message() );
} else {
return "The family was successfully";
}
}
Thank you so much
How to create custom wordpress page after install my custom integration wordpress plugin.
Ex : (If we have install Woocommerce it's automatically create the cart & checkout & myaccount page). I need like some format.
You can create new page on plugin activation hook in your plugin
function add_my_custom_page() {
// Create post object
$my_post = array(
'post_title' => wp_strip_all_tags( 'My Custom Page' ),
'post_content' => 'My custom page content',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'add_my_custom_page');
try this to your plugin
It will check that a page with the same name does not exist.
register_activation_hook( __FILE__, 'custom_plugin_activation' );
function custom_plugin_activation() {
if ( ! current_user_can( 'activate_plugins' ) ) return;
global $wpdb;
if ( null === $wpdb->get_row( "SELECT post_name FROM {$wpdb->prefix}posts WHERE post_name = 'new-page-slug'", 'ARRAY_A' ) ) {
$current_user = wp_get_current_user();
// create post object
$page = array(
'post_title' => __( 'New Page' ),
'post_status' => 'publish',
'post_author' => $current_user->ID,
'post_type' => 'page',
);
// insert the post into the database
wp_insert_post( $page );
}
}
Here is a full list of parameters accepted by the wp_insert_post function.
I developed a plugin that reads a csv file in order to create 4 different custom taxonomies (Categories) and 1 custom post type (Post) using PHP and WordPress.
On the Initial import the execution time is less than 6 seconds, however the second time execution takes more than 1 minute in order to reset and re-upload the data.
The issue that I found is on the resetAllPosts function (see code)
public function resetAllPosts() {
$query = new WP_Query( array(
'post_type' => 'psp',
'post_status' => 'publish',
'posts_per_page' => 100
) );
// remove all taxonomies will take too long..
// delete all custom taxonomies terms
$terms_payment = get_terms( 'payment', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_country = get_terms( 'country', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_currency = get_terms( 'currency', array( 'fields' => 'ids', 'hide_empty' => false ) );
$terms_provider = get_terms( 'provider', array( 'fields' => 'ids', 'hide_empty' => false ) );
foreach ( $terms_payment as $value ) {
wp_delete_term( $value, 'payment' );
}
foreach ( $terms_country as $value ) {
wp_delete_term( $value, 'country' );
}
foreach ( $terms_currency as $value ) {
wp_delete_term( $value, 'currency' );
}
foreach ( $terms_provider as $value ) {
wp_delete_term( $value, 'provider' );
}
while ( $query->have_posts() ) {
$query->the_post();
$post_id = get_the_ID();
// remove the relation between the post and the taxonomies
//wp_delete_object_term_relationships(get_the_ID(),array("provider","payment","currency","country"));
// Remove the feature image from Media gallery
//$post_thumbnail_id = get_post_thumbnail_id( $post_id );
//wp_delete_attachment( $post_thumbnail_id, true );
// delete the post
wp_delete_post( $post_id, "true" );
}
echo "Reset of all data success..";
}
I would like to know the best/fastest way to delete all terms data.
I need a list, in wp-admin, on the profile page, which lists all the users posts. Not just post_type post, but also with author with the current user ID.
I've set the relationshop to the post_type, but isn't it possible to make another "meta query" of some sort, so I can chose only to show the current users posts?
The answer is to create an ACF relationshop query.
function profile_relationship_query( $args, $field, $post_id ) {
$post_type = $args["post_type"][0];
$args = array(
'numberposts' => 10,
'post_type' => $post_type,
'meta_query' => array (
array (
'key' => 'authors',
'value' => '"' . $post_id . '"',
'compare' => 'LIKE'
)
)
);
return $args;
}
add_filter('acf/fields/relationship/query/name=profile_articles', 'profile_relationship_query', 10, 3);
And add it to functions.php.