jarvis Posted August 13, 2020 Share Posted August 13, 2020 Hi All, I really hope someone can help me on this. Basically, I'm trying to disable a bunch of accounts linked by a custom field value of company_id. It toggles a custom field of enable_account from 1 to 0 - this part works fine. What I also need to do, is once the user is disabled, locate any posts by the user (author ID) and set it from publish to draft. Now, this is oddly the part not working! If I dump the code out in any file its fine, however, once I put the exact code back to my functions file, it fails. So I have the disable link on a template like so: <a href="#" data-id="<?php echo $user_info->ID; ?>" data-nonce="<?php echo wp_create_nonce('my_disable_account_nonce') ?>" class="disable-account">Disable</a> It s handled by this: $('.disable-account').click(function(){ if (confirm('Are you sure you want to disable this account?')) { var action = 'disable_account'; var id = $(this).data('id'); var nonce = $(this).data('nonce'); //alert('ID: '+id+' Nonce: '+nonce); $.ajax({ type : "POST", data : { action:action, id:id, nonce:nonce}, dataType : "html", url : '<?php echo admin_url('admin-ajax.php');?>', success : function(data) { //alert(this.data); jQuery("#table_data").html(data); console.log("action:" + action + " id: " + id + " nonce: " + nonce); //debug //window.location.reload(); }, error : function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); } }); } }); Finally, here's the function: function disable_account() { $permission = check_ajax_referer( 'my_disable_account_nonce', 'nonce', false ); if( $permission == false ) { echo '<td colspan="7">Error!</td>'; } else { $response = ''; // Get the company ID $company_id = get_field( 'company_id', 'user_'.$_REQUEST['id'] ); $response .= '<p>Company ID: '.$company_id.'</p>'; // Update the main user update_user_meta( $_REQUEST['id'], 'enable_account', '0' ); $response .= '<p>Primary User ID: '.$_REQUEST['id'].'</p>'; $user_args = array( 'role__in' => array('subscriber', 'staff_member'), 'orderby' => 'meta_value', 'meta_key' => 'company_name', 'order' => 'ASC', 'meta_query'=> array( array( 'relation' => 'AND', # OR array( 'key' => 'enable_account', 'value' => '1', 'compare' => "=", ), array( 'key' => 'company_id', 'value' => $company_id, 'compare' => "=", ) ) ) ); $users = get_users( $user_args ); if ($users): foreach ( $users as $user ) : $user_info = get_userdata( $user->ID ); // Update the main user update_user_meta( $user->ID, 'enable_account', '0' ); #echo '<p>User ID: '.$user->ID.'</p>'; $response .= '<p>User ID: '.$user->ID.'</p>'; global $post; // Get all posts by author ID $myposts = get_posts( array( 'post_status' => 'publish', 'posts_per_page'=> -1, 'author' => $user->ID )); if ( $myposts ) { foreach ( $myposts as $post ) : setup_postdata( $post ); $response .= '<p>Post ID: '.$post->ID.'</p>'; endforeach; wp_reset_postdata(); } endforeach; else: endif; echo $response; } die(); } // Fire AJAX action for logged in users only add_action('wp_ajax_disable_account', 'disable_account'); As I say, using this in an ordinary template works. For now, I've removed the code to change the post from publish to draft as at this stage, its not even displaying the Post ID, so I know its not even getting this far! Have I missed something obvious? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/311324-wordpress-ajax-get-posts-not-working/ Share on other sites More sharing options...
jarvis Posted August 13, 2020 Author Share Posted August 13, 2020 Apologies, why is it by posting I managed to work these things out Each of my posts when created gets the company ID assigned. So rather than query post by author ID, I queried for all posts linked by company ID This worked! More so because the tests I were doing were based on posts written by the primary user ID, not any other user IDs with the same company ID Problem solved! Quote Link to comment https://forums.phpfreaks.com/topic/311324-wordpress-ajax-get-posts-not-working/#findComment-1580586 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.