Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Combine privileged users array with my current query WordPress

  • SOLVED

I have a custom page template with the following query. Isotope filers my categories and all this works fine. I want now to make this page only viewable by a few specific, currently logged in authors. <em>Example: Posts are only visible to an author with one of these ID's (1,2 or 3) and are logged in.</em>


<ul id="filters">
<li><a href="#" data-filter="*">Everything</a></li>
<?php
$terms = get_terms("category"); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>

<?php $the_query = new WP_Query( 'posts_per_page=20' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> item">

<?php the_title(); ?>

</div> <!-- end item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
<?php endif; ?>


I am able to achieve the privileged user's array using the following method: But I have no idea how to combine this with my query listed above.

<?php
$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user

// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) {
$query = array(
'posts_per_page' => 20,
'author__in' = $authors
);
$the_query = new WP_Query( $query );
}
?>

<?php if ( $query ) : ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<?php the_title(); ?>

<?php endwhile;?>
<?php else : ?>
This message will be displayed if the signed in user is not in the authors array.
<?php endif; ?>


<strong>Please provide the full combined code.</strong>

Answers (4)

2014-10-15

Hariprasad Vijayan answers:

Hi,

Try this code

<?php
$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user
// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) {
?>
<ul id="filters">
<li><a href="#" data-filter="*">Everything</a></li>
<?php

$terms = get_terms("category"); // get all categories, but you can use any taxonomy

$count = count($terms); //How many are they?

if ( $count > 0 ){ //If there are more than 0 terms

foreach ( $terms as $term ) { //for each term:

echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";

//create a list item with the current term slug for sorting, and name for label

}

}

?>
</ul>
<?php $the_query = new WP_Query( 'posts_per_page=20' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();

$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item

$termsString = ""; //initialize the string that will contain the terms

foreach ( $termsArray as $term ) { // for each term

$termsString .= $term->slug.' '; //create a string that has all the slugs

}

?>
<div class="<?php echo $termsString; ?> item">
<?php the_title(); ?>
</div>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
<?php } ?>

The above code only display the template if the logged in user id is 1,2 or 3,
If you only want to show the posts for the specific users, you can try the following code

<ul id="filters">
<li><a href="#" data-filter="*">Everything</a></li>
<?php

$terms = get_terms("category"); // get all categories, but you can use any taxonomy

$count = count($terms); //How many are they?

if ( $count > 0 ){ //If there are more than 0 terms

foreach ( $terms as $term ) { //for each term:

echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";

//create a list item with the current term slug for sorting, and name for label

}

}

?>
</ul>
<?php
$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user
// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) {
?>
<?php $the_query = new WP_Query( 'posts_per_page=20' ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();

$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item

$termsString = ""; //initialize the string that will contain the terms

foreach ( $termsArray as $term ) { // for each term

$termsString .= $term->slug.' '; //create a string that has all the slugs

}

?>
<div class="<?php echo $termsString; ?> item">
<?php the_title(); ?>
</div>
<!-- end item -->
<?php endwhile; ?>
</div>
<!-- end isotope-list -->
<?php endif; ?>
<?php } ?>


Jimmy Shepard comments:

It's still showing posts to people who are not in the array.


Hariprasad Vijayan comments:

Okay. Try this.
<ul id="filters">
<li><a href="#" data-filter="*">Everything</a></li>
<?php
$terms = get_terms("category"); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php
$authors = array( 1, 2, 3 ); // privileged users array
$signed_in_user = wp_get_current_user(); // get current user
// check if singed in user is in authors array
if ( in_array( $signed_in_user->ID, $authors ) ) {
$query = array(
'posts_per_page' => 20,
'author__in' = $authors
);
?>
<?php $the_query = new WP_Query( $query ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="isotope-list">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> item">
<?php the_title(); ?>
</div>
<!-- end item -->
<?php endwhile; ?>
<?php else: ?>
This message will be displayed if the signed in user is not in the authors array.
<!-- end isotope-list -->
<?php endif; ?>
</div>
<?php } ?>


Jimmy Shepard comments:

Thanks, but I'm getting a blank page.


Hariprasad Vijayan comments:

Getting any error?
Have you tried with logged in user with id 1,2 or 3?


Jimmy Shepard comments:

Yes, still just blank. No error messages. I'm signed in as user 1.


Hariprasad Vijayan comments:

Check is there is any syntax error when you copy code from here as Dbranes said.


Jimmy Shepard comments:

There is a single equal sign '=' instead of '=>' here:
array( 'posts_per_page' => 20, 'author__in' = $authors );
I'll change that and see what happens.

2014-10-15

Arnav Joy answers:

try this

<ul id="filters">

<li><a href="#" data-filter="*">Everything</a></li>

<?php

$terms = get_terms("category"); // get all categories, but you can use any taxonomy

$count = count($terms); //How many are they?

if ( $count > 0 ){ //If there are more than 0 terms

foreach ( $terms as $term ) { //for each term:

echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";

//create a list item with the current term slug for sorting, and name for label

}

}

?>

</ul>


<?php
$authors = array( 1, 2, 3 ); // privileged users array

$signed_in_user = wp_get_current_user(); // get current user



// check if singed in user is in authors array



$the_query = new WP_Query( 'posts_per_page=20' ); ?>

<?php if ( $the_query->have_posts() && in_array( $signed_in_user->ID, $authors ) ) : ?>

<div id="isotope-list">

<?php while ( $the_query->have_posts() ) : $the_query->the_post();

$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item

$termsString = ""; //initialize the string that will contain the terms

foreach ( $termsArray as $term ) { // for each term

$termsString .= $term->slug.' '; //create a string that has all the slugs

}

?>

<div class="<?php echo $termsString; ?> item">



<?php the_title(); ?>



</div> <!-- end item -->

<?php endwhile; ?>

<?php else : ?>

you are not a previlaged user

</div> <!-- end isotope-list -->

<?php endif; ?>


Jimmy Shepard comments:

In trying this- users who are not 1, 2 or 3 get the message "you are not a privileged user" which is good.
However, when the array has only users 4, 5 and 6 they can still see 1, 2 and 3's posts.


Arnav Joy comments:

Sorry , I did not understood following "However, when the array has only users 4, 5 and 6 they can still see 1, 2 and 3's posts."
Please let me know.


Jimmy Shepard comments:

On my custom page, users 1, 2 and 3 should be able to see the posts of only users 1, 2 and 3.
like this: $authors = array( 1, 2, 3 ); // privileged users array

I have another user who's id is 4 and he is not listed in that code, but when he logs into the site he can see the posts from users 1, 2 and 4 as well as his own.

2014-10-16

timDesain Nanang answers:

Hi Sir,
try this code:


<?php
global $user_ID;
$authors_A = array( 1, 2, 3 );
$authors_B = array( 4, 5, 6 );
$authors_C = array( 7, 8, 9 );

if(user_can($user_ID, 'edit_posts') AND in_array($user_ID, array_merge($authors_A, $authors_B, $authors_C))){
if(in_array($user_ID, $authors_A)) $post_authors = $authors_A;
elseif(in_array($user_ID, $authors_B)) $post_authors = $authors_B;
elseif(in_array($user_ID, $authors_C)) $post_authors = $authors_C;

?><ul id="filters"><?php
?><li><a href="#" data-filter="*">Everything</a></li><?php
$terms = get_terms("category");
if ( count($terms) > 0 ){
foreach ( $terms as $term ) {
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
}
}
?></ul><?php

$args = array(
'posts_per_page' => 20,
'author__in' => $post_authors
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
?><div id="isotope-list"><?php
while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
if(is_array($termsArray)){
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
}
?> <div class="<?php echo $termsString; ?> item"> <?php
the_title();
?></div> <?php
endwhile;
?></div> <!-- end isotope-list --> <?php
endif;
}
else{
echo 'restricted page';
}
?>


Jimmy Shepard comments:

This is excellent! This seems to mean I can use just one page template and assign several different user groups.
Group 123, Group 456, Group 789.
The page template would display posts relevant to only it's group. <strong>and no group can see the other's posts</strong>.
Is that right?

2014-10-15

Dbranes answers:

You can seperate the access restriction from your template code, with only few lines of code:


/**
* Only allow some users to access to the page 'some-page-slug'.
* @see http://www.wpquestions.com/question/showChronoLoggedIn/id/9974
*/

add_action( 'template_redirect', 'wpq_restrict_access' );

function wpq_restrict_access() {

// ------------------------------------------
// Edit the following to your needs:
//
$users_ids_with_access = array( 1, 2, 3 );
$pages_to_restrict = array( 'some-page-slug' );
// -------------------------------------------

if( is_page( $pages_to_restrict ) ) {
if( ! in_array( get_current_user_id(), $users_ids_with_access, true ) ) {
echo '<h1>Restricted Access!</h1>';
exit();
}
}
}


Then you can add <em>get_header()</em> and <em>get_footer()</em> to your needs. You could also use a custom template part, for example: <em>get_template_part( 'restricted-access' )</em> where you have to create the file <em>restricted-access.php</em> in your current theme directory.

You can also do similar with the <em>template_include</em> filter.


Jimmy Shepard comments:

So if I'm using a custom page template I wound put the template page name in place of "some-page-slug" and add it to fuctions.php?

I'll try this out and get back to you after lunch.


Dbranes comments:

ok, sure

If your page url is <em>example.com/abc</em> then the page slug to use is <em>abc</em>.

You can also use the page id instead of the slug (for example 123).


ps: so we are not checking the name of the template here, but we can do that via the template_include filter.


Dbranes comments:

Yes you can add it to your functions.php file or better yet create a plugin for it, if you want this to hold when you switch themes.


Jimmy Shepard comments:

Hi Dbranes,
I added the code you provided to my funtions.php. I replaced "some-page-slug" with "test1" which is my page slug.
I'm getting a blank page. -in fact the whole site is blank until I removed the function.
Any ideas?

I'm on WordPress 4.0


Dbranes comments:

Most likely a PHP syntax error you got with blank page,

be careful when copying code from this site, it will add extra new lines.

I assumed you used PHP 5.3+, with anonymous function.

I updated the code with to support older PHP versions.


Jimmy Shepard comments:

Ok, that fixed the blank page issue.
So, as it stands, User id 4 gets "Restricted Access!" message which is great. The problem now is that Users 1, 2 and 3 can view user 4's posts. I'm trying to have it so that if your are in the 1,2,3 group, you can only see posts from ids 1. 2 and 3.