0

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. Example: Posts are only visible to an author with one of these ID's (1,2 or 3) and are logged in.

    <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; ?>
0

Just add this before your code

$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 ) ) :

And then this after your code

<?php else : ?>
This message will be displayed if the signed in user is not in the authors array.
<?php endif; ?>
| improve this answer | |
  • Sanchy, thanks for the help, but would you mind posting your code combined with mine though? I'm getting a blank page. I tried the first code you provided above and then below the filter and still a blank page. Or maybe I should have replaced the last code you gave with my other php endif thingy? – Jimmy Oct 14 '14 at 21:28
  • Do you know PHP? Blank page without errors? – bysanchy Oct 14 '14 at 22:09
  • Yes, a blank page- that's all. – Jimmy Oct 14 '14 at 23:15
  • Well if you know php you'll be able to find out why is the page blank. I can't help you from here – bysanchy Oct 15 '14 at 21:15
  • Not very helpful @sanchy - I'm here to ask for help. The full code is what I need. Not pieces of a puzzle that I have to put together. – Jimmy Oct 16 '14 at 0:46
0

A working solution to help someone in the future:

    <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 } ?>

Thank you Wpquestions.com :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.