This topic has 4 replies, 3 voices, and was last updated 6 years by Radu.

  • Author
  • #206791
     kcurlsjr
    Participant

    Hello,

    I am using the membership plugin to restrict access to posts based on membership plans.  It works well on the posts page and using ‘pre_get_posts’ action hook I am able to customize the WP query  to exclude the restricted posts.

    However on the home page , we have the ‘Recent Blog Post’ Segment which is nothing but ‘Kleo Posts’ element. Is there a way I can use ‘pre_get_posts’ action hook to customize the query used in ‘Kleo Posts’ to filter out certain post IDs that should not be displayed to a user without the right membership.

    Please do let me know how this can be done. Also let me know of any information you need from my end to process this ticket.

    Thank You

    #206798
     Laura
    Moderator

    Hello, will assign the ticket to a higher support level who can help and advise you in your query.
    Thanks! ?

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    Laura Solanes - Graphic Designer and Web Designer

    Please be patient as I try to answer each topic as fast as i can.

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Always happy to help you 🙂

    #207008
     Radu
    Moderator

    Hi,

    We are using the Visual composer logic for that shortcode and you can inspire from here : https://theme.co/apex/forums/topic/exclude-category-from-recent-posts-in-visual-composer/

    Let me know
    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #207125
     kcurlsjr
    Participant

    Hi,

    Thank you @Laura and @Radu for your response.


    @Radu
    I tried the solution suggested , however I don’t know why but because ‘Kleo Posts’ is a custom query, the method I am calling on action hook ‘pre_get_posts’ is unable to filter the query.
    Please find below the code I am using to hide restricted posts:
    ************************************************
    add_action( ‘pre_get_posts’, ‘pmsc_exclude_post_from_query’ );
    function pmsc_exclude_post_from_query( $query ) {

    if( !is_admin() && $query->is_main_query() && function_exists(‘pms_is_post_restricted’) ) {
    $args = $query->query_vars;
    $args[‘suppress_filters’] = true;
    $args[‘posts_per_page’] = get_option( ‘posts_per_page’ );
    $posts = get_posts($args);

    $ids = wp_list_pluck( $posts, ‘ID’ );
    $restricted_ids = array_filter($ids,’pms_is_post_restricted’);

    $query->set( ‘post__not_in’, $restricted_ids );
    }
    }
    ************************************************************************************
    In the above code : pms_is_post_restricted is a function defined inside the membership plugin files that checks if the current user has any posts they are restricted from . Then by using array_filter we are able to get a list of post IDs that should not be included in the query.

    One possible solution:

    Can a hook to be added inside: kleo/vc_templates/vc_posts_grid.php over the WP_Query args? If so then I can modify the query in kleo posts before its called.

    Please do let me know.

    Thank You

    #207504
     Radu
    Moderator

    Hi,

    If you need to change that logic you can copy this file ( wp-content/themes/kleo/vc_templates/vc_posts_grid.php ) to child theme ( wp-content/themes/kleo-child/vc_templates/vc_posts_grid.php ) and make your desired changes.

    Doing changes in the child theme using your custom $args will result the same without using filter or action hook.

    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
Viewing 5 posts - 1 through 5 (of 5 total)

The forum ‘Bugs & Issues’ is closed to new topics and replies.

Log in with your credentials

Forgot your details?