[Resolved] Filtering category views to showpost pages

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Filtering category views to showpost pages

This topic contains 5 replies, has 2 voices, and was last updated by  Tom 5 days, 16 hours ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #10599

    Ian
    Participant

    Hi – This is a lovely filter to switch category views into a show posts page. It is a bit ‘fragile’ because there’s no error handling. I’ve inserted a conceptual query with an enquiry below. Forgive me for asking but I’m a wordpress baby – although not a php one – and I’m finding my way round the code with care…Apart from the case where the page has a very large number of pages which would present a performance overhead issue – would this be an useful strategy?

    add_filter('request', function( array $query_vars ) {
        if ( is_admin() ) {
            return $query_vars;
        }
    
        if ( isset( $query_vars['category_name'] )) {
    

    PSEUDOCODE: iterate through array of pagenames. If == found between pagename and $query_vars[‘category’] execute inner if loop ELSE RETURN

    
            if ( isset( $query_vars['category_name'] ) ) {
               $pagename = $query_vars['category_name'];     $query_vars = array( 'pagename' => "$pagename" );
             }
      
        }
        return $query_vars;
    } );
    #10622

    Tom
    Keymaster

    Hi there,

    I’m not too sure what you mean – it looks the same as the code we have here: https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/

    What about it did you change, exactly?

    #10659

    Ian
    Participant

    Sorry. I didn’t change the code. I was suggesting that when I wrote ‘pseudocode’ iterate through an array of pagenames, there was a opportunity to put an error handling loop in which refuses to execute if there is no matching slug for the the filter to act on. I’m just thinking of the real world case where muggins who shouldnt changes the slug of a category replacement page. But I don’t know. The effect of being able to put show posts into the category page is awesome and your work is much appreciated

    #10663

    Tom
    Keymaster

    Ah, good point.

    I wonder if this would work?:

    add_filter('request', function( array $query_vars ) {
        if ( is_admin() ) {
            return $query_vars;
        }
    
        if ( isset( $query_vars['category_name'] ) ) {
            $pagename = $query_vars['category_name'];
    
            if ( get_page_by_title( $pagename ) ) {
                $query_vars = array( 'pagename' => "$pagename" );
            }
        }
    
        return $query_vars;
    } );

    Let me know 🙂

    #10705

    Ian
    Participant

    Dear Tom,

    Thank you for suggesting that nested IF code. I can confirm that it handles the error ‘silently’ and swiftly which is what one would want – unless I guess you are trying to error handle why the filtered ShowPosts category page doesn’t appear… But yes it works. You can check out what I’m up to here:

    #10721

    Tom
    Keymaster

    Awesome, glad it’s working 🙂

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.