[Resolved] Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Home Forums Support [Resolved] Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Home Forums Support Related Posts by Tags and Categories [WP Show Posts Pro + GP Hooks]

Viewing 12 posts - 31 through 42 (of 42 total)
  • Author
    Posts
  • #1290961
    Christian

    lol, sorry. Not sure what’s up with me today.

    Here it is:

    array(7) { [“order”]=> string(4) “desc” [“orderby”]=> string(4) “rand” [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(6) [“ignore_sticky_posts”]=> bool(true) [“post_status”]=> array(1) { [0]=> string(7) “publish” } [“post__not_in”]=> array(1) { [0]=> int(5621) } }

    #1292350
    Tom
    Lead Developer
    Lead Developer

    So our taxonomy stuff isn’t making it into the query at all. That’s very strange – not sure what could be conflicting there.

    <div class="wpsp-related-posts1  grid-container">
    	<h2>Related Posts</h2>
    <?php
        $tax = 'post_tag';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            $tag_ids = [];
            foreach ($tags as $tag) {
                $tags_list[] = $tag->slug;
                $tag_ids[] = $tag->term_id;
            }
    
            $args = array(
                'numberposts' => 5,
                'tag__in' => $tag_ids,
                'post__not_in' => array( get_the_ID() )
            );
    
            $posts = get_posts( $args );
    
            if ( ! empty( $posts ) ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
                $text_term = $cats[0]->slug;
                $tax = 'category';
            }
        }
        var_dump('taxonomy=' . $tax . '&tax_term=' . $tax_term);
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>

    What does this output?

    #1292460
    Christian

    Would you like me to keep the filter?

    With the filter:

    string(27) “taxonomy=category&tax_term=”
    array(7) { [“order”]=> string(4) “desc” [“orderby”]=> string(4) “rand” [“post_type”]=> string(4) “post” [“posts_per_page”]=> int(6) [“ignore_sticky_posts”]=> bool(true) [“post_status”]=> array(1) { [0]=> string(7) “publish” } [“post__not_in”]=> array(1) { [0]=> int(5621) } }

    Without the filter:
    string(27) “taxonomy=category&tax_term=”

    #1292894
    Tom
    Lead Developer
    Lead Developer

    Lol, I spelled the variable name wrong, unbelievable.

    Try this:

    <div class="wpsp-related-posts1  grid-container">
    	<h2>Related Posts</h2>
    <?php
        $tax = 'post_tag';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            $tag_ids = [];
            foreach ($tags as $tag) {
                $tags_list[] = $tag->slug;
                $tag_ids[] = $tag->term_id;
            }
    
            $args = array(
                'numberposts' => 5,
                'tag__in' => $tag_ids,
                'post__not_in' => array( get_the_ID() )
            );
    
            $posts = get_posts( $args );
    
            if ( ! empty( $posts ) ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
                $tax_term = $cats[0]->slug;
                $tax = 'category';
            }
        }
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>
    #1292918
    Christian

    Smh, Tom…

    Kidding. Okay, I think you got it. This one’s working perfectly! πŸ™‚

    Thank you so much, Tom!

    #1293719
    raysn

    Hmm I am getting the following php error message when using the code mentioned above:

    Warning: Invalid argument supplied for foreach() in /www/htdocs/xxxxxx/xxxxxx/wp-content/plugins/gp-premium/elements/class-hooks.php(180) : eval()’d code on line 24

    Seems like this error only occurs, when there is no tag set.

    By the way: I’m using the related posts element within the before_right_sidebar_content hook.

    The Plugin query monitor outputs the following information:
    Error information

    #1294168
    Tom
    Lead Developer
    Lead Developer

    Here it is cleaned up:

    <div class="wpsp-related-posts1  grid-container">
        <h2>Related Posts</h2>
        <?php
        $tax = 'post_tag';
        $tax_term = '';
    
        if ( is_single() ) {
            $tags =  get_the_tags();
            $tags_list = [];
            $tag_ids = [];
            foreach ( (array) $tags as $tag) {
                if ( isset( $tag->slug ) ) {
                    $tags_list[] = $tag->slug;
                    $tag_ids[] = $tag->term_id;
                }
            }
    
            $args = array(
                'numberposts' => 5,
                'tag__in' => $tag_ids,
                'post__not_in' => array( get_the_ID() )
            );
    
            $posts = get_posts( $args );
    
            if ( ! empty( $posts ) && ! empty( $tag_ids ) ) {
                $tax_term = implode( ', ', $tags_list);
            } else {
                $cats =  get_the_category();
    
                if ( ! empty( $cats ) ) {
                    $tax_term = $cats[0]->slug;
                }
    
                $tax = 'category';
            }
        }
    
        $list = get_page_by_title( 'related', 'OBJECT', 'wp_show_posts' );
        wpsp_display( $list->ID, 'taxonomy=' . $tax . '&tax_term=' . $tax_term );
        ?>
    </div>

    Let me know if that fixes it or not πŸ™‚

    #1294691
    Christian

    Whoops, I keep forgetting to mark these as resolved, heh. πŸ™‚

    #1303156
    raysn

    Hey Tom,

    although there is no critical error anymore, I still get 2 minor PHP issues (“Trying to get property of non-object”). I made a screenshot of the messages (have a look on the full-size image).

    Greetings
    Raysn

    Related Posts 2 minor PHP issues

    #1303823
    Tom
    Lead Developer
    Lead Developer

    Likely means there are no categories/tags in your case. Updated the code to check for empties: https://generatepress.com/forums/topic/related-posts-by-tags-and-categories-wp-show-posts-pro-gp-hooks/page/3/#post-1294168

    #1545669
    Andrei

    Hey guys, I’m having a similar issue with no posts available to be shown. I follow this guide https://gpsites.co/merch/the-single-post/ – How I made the Related Posts. It works great just some of my categories have 1 article and I get that no posts available notice.

    What I would like to achieve in the end would be to display related posts by category and if none available to have a fallback on most recent published articles.

    Is this possible?

    Cheers.

    #1546982
    Elvin
    Staff
    Customer Support

    Hi Andrei,

    As this is already resolved for the topic starter, can you open up a new topic?

    So you could use the Private information text field incase needed. Thank you. πŸ™‚

Viewing 12 posts - 31 through 42 (of 42 total)
  • You must be logged in to reply to this topic.