Redwall Redwall - 1 month ago 7
PHP Question

I need to check if Current user is Author to Custom Post Type

I want to write a conditional statement (in pseudo below to explain)

IF current logged in user is 'Author' to a custom-post-type called 'farmers' {
// Do something
} else {
// Do something else
}


My code effort so far is

<?php
// Global variable for current user ID
$user_ID;

// Create a new WP query object
$my_query = new WP_Query( array(
'post_type' => 'farmers',
'author' => $user_ID
));

// Get all the current user posts from the query object
$posts = $my_query->posts;

IF $posts = 0 {
// Do something
} else {
// Do Something else
} ?>


User can have the role "Author" in the system, but not necessarily have any posts. So I need to check the condition that this Author actually has a post. I am just not sure how to write that IF statement at the end of the code and looking for some help.

Thanks

Answer

Use something like this

$query="select * from posts where type='farmers'";
$result=mysql_query($query);
$valid=false;
if(mysql_num_rows($result)>0){
$valid=true;
}


if($_SESSION["id"]==$posts["id"]){
//your code
}else{
//your code
}

or

if(mysql_num_rows($posts)>0){
// user have post
 }
Comments