0

I've got this bit of code in my functions.php :

  // Disable theme single views
function fq_disable_single_cpt_views() {
  $queried_post_type = get_query_var('post_type');
  $cpts_without_single_views = array( 'ait-item' );
  if ( is_single() && in_array( $queried_post_type, $cpts_without_single_views )  ) {
      wp_redirect( home_url( '/sorry' ), 301 );
    exit;
  }
}

add_action( 'template_redirect', 'fq_disable_single_cpt_views' );'

Which is redirecting single-page requests of a particular post-type to a created page. So if someone goes to a category, clicks the posts permalink they get directed to a dedicated page instead.

What I'm now trying to do is only apply the above, if the post is NOT featured. So if they post IS featured it still leads to the real post, not redirected.

The trouble im having is understanding if this can be done from the functions.php. The theme files use the Nette framework which I'm not too confident with. This is a snippet from the theme which shows the "isfeatured" query. I'm wondering how I can use an if statement to do this ?

{var $dbFeatured = get_post_meta($post->id, '_ait-item_item-featured', true)}

======================================================

**

Additional Question unanswered.

**

Thank you for answering the above, The answer is below for anyone wanting the get_post_meta.

I'm now wondering if its possible to use user roles instead of get_post_meta, so if a certain post belong to a user, who's role is not a verified member, they get redirected, but if the post belongs to a verified member it goes to the main profile.

Using the solution below, I've tried this code but i can't see what im doing wrong ?

function fq_disable_single_cpt_views() {
    $queried_post_type = get_query_var('post_type');
    $cpts_without_single_views = array( 'ait-item' );

$user_meta=get_userdata($user_id); $user_roles=$user_meta->roles; if (in_array("cityguide_84d6da05", $user_roles)){}

    if (    is_single()
        &&  in_array( $queried_post_type, $cpts_without_single_views )
        &&  ("0" === $_ait_item_item_featured)
    ) {
        wp_redirect( home_url( '/sorry' ), 301 );
        exit;
    }

}

add_action( 'template_redirect', 'fq_disable_single_cpt_views' );

For your reference , cityguide_84d6da05 is the user role of a verified member.

  • what are the possible values of _ait-item_item-featured ? – mmm Sep 27 '17 at 18:30
  • Sorry Im not much of a coder, I muddle through. But if im correct and looking at SQL , the value is 1 and 0 . Which represents a switch to say yes its featured, or no it isnt . – Randomer11 Sep 27 '17 at 18:34
  • for the additional question, you can use user_can to test the role of a user. then to test the role of the author, use if (user_can($post->post_author, "cityguide_84d6da05")) { ... developer.wordpress.org/reference/functions/user_can – mmm Sep 27 '17 at 20:06
  • Sorry could I trouble you for an example/answer using the code, I've tried understading the user_can but I can't seem to get it to work. – Randomer11 Sep 27 '17 at 21:01
1

to add a condition on a meta value, you can modify the test like that :

function fq_disable_single_cpt_views() {

    $queried_post_type = get_query_var('post_type');
    $cpts_without_single_views = array( 'ait-item' );

    $_ait_item_item_featured = get_post_meta(get_the_ID(), "_ait-item_item-featured", TRUE);

    if (    is_single()
        &&  in_array( $queried_post_type, $cpts_without_single_views )
        &&  ("0" === $_ait_item_item_featured)
    ) {
        wp_redirect( home_url( '/sorry' ), 301 );
        exit;
    }

}
  • Thank you, that works perfectly, I've marked the answer as useful. :) Doing more research on the "packages" mentioned above, it looks like each package creates a user role, so I guess the ideal would be a conditional based on user role ? – Randomer11 Sep 27 '17 at 19:23

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.