Check if current user has a post (CPT) in custom taxonomy
-
I have been trying to work out how to write a function to check if the current user already has a post in a category/taxonomy, and then if so, redirect them to another page so they are unable to post in that category again until they delete their other post. The user posts from the front end, and this is with a plugin (Classified Listing), which has its own post type (rtcl_listing) and I have created (within the plugin) various Ad Types and Categories. I have one specific Ad Type (Members) which only has one category (Member Pages), where I only want users who qualify to be able to create one post. I have two other Ad Types with their own categories that are not limited. I have managed to set it up so that only the users who qualify to create Member Pages can access the listing form to do so, but so far I haven’t been able to find a way to prevent those who have already created a Member Page from creating a second (or third, fourth etc) one without my manual intervention, and I want any attempt to do so prevented before it is tried, automatically. I have tried various ways to try and create a function involving either a wordpress query or SQL query to check or count the posts the user has and then redirect if the count is greater than zero, but because the required data is split across two or three tables, in combination with it being a custom post type I am finding it impossible to do. I am absolutely certain that the latest function I have tried is not correctly coded, but I am pasting it below in the hope that it is not a million miles away from what I need. I suspect that it is the count posts bit that is wrong but it is the only thing I could find, but I am a novice at this so would not be surprised if something else is wrong. The function is called at the start of the listing form but does not do anything at present. Any help or suggestions would be greatly appreciated. I should add that this is not a feature offered by the plugin nor intended to be, so I thought that this is the right forum to ask in, but apologies if it is not.
/*Add function to redirect user when already has Member Page*/ function member_redirect() { $args = array( 'post_type' => 'rtcl_listing', 'ad_type' => 'member', 'post_author' => ''); $posts = query_posts($args); $user_id = get_current_user_id(); if ( ($user_id = 'post_author') && ( is_page( 'listing-form/?category=44&type=member' ) ) && (wp_count_posts($posts) > 0)) { wp_redirect( home_url("/listing-form/limit-reached/") ); } } add_action( 'member_page_exists', 'member_redirect' );
- The topic ‘Check if current user has a post (CPT) in custom taxonomy’ is closed to new replies.