Support » Developing with WordPress » Check if current user has a post (CPT) in custom taxonomy

  • Resolved AWOL

    (@awol)


    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' );
    • This topic was modified 2 years, 6 months ago by AWOL.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Include the current user ID as an ‘author’ (not ‘post_author’) arg, assuming that’s how this CPT works. Also use ‘tax_query’ or similar arg to only get listings in that particular category, i.e. attempt to get the one post the user is presumably trying to inappropriately make again. If the query fails, then there is no such listing. Do not redirect.

    Do not use query_posts(), that resets the default query, which doesn’t sound like your intent. Instead use get_posts() or create a new WP_Query object.

    I’m unsure what the correct hook would be, which may alter how your code works. That’s something to confirm through the plugin’s dedicated support forum. If the hook fires after output has started, you cannot redirect with PHP, it’ll have to be done via JavaScript.

    Thread Starter AWOL

    (@awol)

    @bcworkz
    Thanks for the tips. I have adjusted the code and changed from trying to do a redirect and instead have created a div that echoes when the conditions are met. I added a new action hook at the start of the listing form, which I already had in my child theme, and after some adjustments to the css I have achieved my goal, as the message appears and blocks the user from completing the form until they have deleted their other listing. In case you or anyone else is interested, here is the function I ended up with. I’m not sure if it is either the correct or most elegant way to do what I wanted, but it works!

    /*Add function to redirect user when already has Member Page*/
    function member_page_exists() {
        $user_id = get_current_user_id();
        $url = home_url( $_SERVER['REQUEST_URI'] );
        $args = array(
        'post_type' => 'rtcl_listing',
        'tax_query'	=> array(
    		array(  'taxonomy'	=> 'rtcl_category',
    			    'field'		=> 'slug',
    			    'terms'		=> 'member_pages' ) ),
        'post_status' => 'publish',
        'author' => $user_id);
        $posts = get_posts($args);
        if ( ( $url = 'URL OF THE LISTING PAGE' ) && (wp_count_posts($posts) > 0)) {
        echo "<div class='above'><div class='member-message'>You have already created a Member Page!<p>Please check the <a href='URL TO EDIT OR DELETE LISTING'>listings in your Dashboard</a> and either edit your existing Member Page, or delete it and then create a new one.</p></div></div>";}
    }
    add_action( 'already_created', 'member_page_exists' );

    At the start of the listing form I put this;
    <?php do_action( 'already_created' ); ?>

    And then the CSS was:

    .above {
    	z-index: 998;
    	height: 100%;
    	width: 100%;
    	background: RGBA(211, 211, 211, 0.8);
    	position: absolute;
    	top: 0;
    	left: 0;
    	padding-top: 10%;
    }
    .member-message {
    	z-index: 999;
    	background: #c8ffe6;
    	color: #008000;
    	padding: 10%;
    	text-align: center;
    	font-size: large;
    	font-weight: bolder;
    	width: 50%;
    	position: relative;
    	margin: 0 auto;
    }

    Hopefully someone else may find it useful.

    Moderator bcworkz

    (@bcworkz)

    Thanks for posting your solution. If it works without error, then it’s correct. Elegance be damned 🙂

    Thread Starter AWOL

    (@awol)

    A slight update, as after further testing the echoed message was appearing for any authorised user regardless of if they had a post in the category or not; it turns out that the wp_count_posts($posts) > 0 was not working, but after changing this to count($posts) > 0 it works properly – fully tested this time!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Check if current user has a post (CPT) in custom taxonomy’ is closed to new replies.