WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Post Type, pre_get_posts and dynamic validation (8 posts)

  1. tiaanswart
    Member
    Posted 1 year ago #

    Hello

    I am trying to dynamically exclude all invalid posts from my custom post type 'show-case'.

    To start of i have the following function to identify the invalid posts:

    function ts1_showcase_invalid() {
        $showcase_invalid = array();
    
        $ts1_showcase_invalid = get_posts(array(
                'numberposts'   =>  -1,
                'post_type'     =>  array('show-case'),
            )
        );
    
        foreach($ts1_showcase_invalid as $post) {
            setup_postdata($post);
            $redirect_url = '';
            $redirect_url = get_post_meta($post->ID, 'show_case_format_meta_redirect_url', true);
            if ( !has_post_thumbnail( $post->ID ) ) {
                array_push( $showcase_invalid , $post->ID );
            } elseif ( !empty($redirect_url) && validateURL($redirect_url) !== 'good' ) {
                array_push( $showcase_invalid , $post->ID );
            }
        }
    
        set_transient( 'showcase_invalid', $showcase_invalid );
        wp_reset_postdata();
    
        return $showcase_invalid;
    }

    Printing the function gives me the expected output:

    Array ( [0] => 613 )

    Then i have the following function that makes use of pre_get_posts to exclude the posts identified by my above function:

    add_filter('pre_get_posts', 'ts1_remove_showcase_invalid');
    function ts1_remove_showcase_invalid( $query ) {
        if( !$query->is_admin || isset($query->query_vars['post_type']) && 'nav_menu_item' == $query->query_vars['post_type'] ) {
            //Stupid pre_get_posts and navigation in WordPress
            return;
        } elseif( !is_admin() ) {
            $query->set( 'post__not_in', ts1_showcase_invalid() );
        }
        return $query;
    }

    The following query is used to display the posts from my custom post type 'show-case':

    $limit = get_option('posts_per_page');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    query_posts('showposts=' . $limit . '&paged=' . $paged .'&post_type=show-case');
    
    $wp_query->is_archive = false; $wp_query->is_home = false;

    However when this page is executed the query variables are as follows:

    Array ( [showposts] => 10 [paged] => 1 [post_type] => show-case [error] => [m] => 0 [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [category_name] => [tag] => [cat] => [tag_id] => [author_name] => [feed] => [tb] => [comments_popup] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [fields] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [update_post_meta_cache] => 1 [posts_per_page] => 10 [nopaging] => [comments_per_page] => 0 [no_found_rows] => [order] => DESC )

    As you can see the 'post__not_in' has no value??

    I am truly lost! Have tried everything i could think about to fix this but with no luck!

    Please help :)

  2. tiaanswart
    Member
    Posted 1 year ago #

    Hello, anyone's help world be greatly appreciated here! :)

  3. keesiemeijer
    moderator
    Posted 1 year ago #

    If you are not using this on a Page template file you can leave out the query_posts and query from the pre_get_posts action.

    Try it with this:

    add_action('pre_get_posts', 'ts1_remove_showcase_invalid');
    function ts1_remove_showcase_invalid( $query ) {
     // not an admin page and only for the main query
      if (!is_admin() && $query->is_main_query()){
    
         // wrap these $query->set in a conditional tag
         $query->set( 'post__not_in', ts1_showcase_invalid());
         $query->set( 'post_type', array('show-case'));
         $limit = get_option('posts_per_page');
         $query->set( 'posts_per_page', $limit);
    
      }
    }

    You'll need to tell WordPress where to use these query vars with a conditional tag: http://codex.wordpress.org/Conditional_Tags

  4. tiaanswart
    Member
    Posted 1 year ago #

    Thanks a mil for the feedback! However isn't there a way that I can exclude these 'invalid' posts from any query on the front end with just one function?

    The conditional tags will limit me to page content and exclude queries performed by my sidebars, like the recent posts widget?

  5. tiaanswart
    Member
    Posted 1 year ago #

    I tried the below as well and it still did not populate the 'post__not_in':

    add_filter('pre_get_posts', 'ts1_remove_showcase_invalid');
    function ts1_remove_showcase_invalid( $query ) {
        if( !$query->is_admin || isset($query->query_vars['post_type']) && 'nav_menu_item' == $query->query_vars['post_type'] ) {
            //Stupid pre_get_posts and navigation in WordPress
            return;
        } elseif( !is_admin() && !ts1_showcase_invalid() ) {
            $query->set( 'post__not_in', ts1_showcase_invalid() );
        }
        return $query;
    }
  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Then try it only with query_posts, without the 'pre_get_posts' action. something like this:

    $limit = get_option('posts_per_page');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $not_in = ts1_showcase_invalid();
    $args = array(
    'posts_per_page' => $limit,
    'post_type' => 'show-case',
    'post__not_in' => $not_in,
    'paged' => $paged,
    );
    query_posts($args);
  7. tiaanswart
    Member
    Posted 1 year ago #

    Thanks again for the reply. So do I edit the code from my query that shows the 'show-case' post items or can I make this a a global filter?

  8. tiaanswart
    Member
    Posted 1 year ago #

    I finally found a solution for this issue... Each time a post is saved or published i check if the posts are valid then change all the invalid posts to draft, here is my code if this helps anyone else.

    /*******************************
     Make Showcase Posts That Are Not Valid Drafts
    ********************************/
    
    function ts1_showcase_invalid() {
        $showcase_invalid = array();
        $ts1_showcase_invalid = get_posts(array(
                'numberposts'   =>  -1,
                'post_type'     =>  array('show-case')
            )
        );
        foreach($ts1_showcase_invalid as $post) {
            setup_postdata($post);
            $redirect_url = '';
            $redirect_url = get_post_meta($post->ID, 'show_case_format_meta_redirect_url', true);
            if ( !has_post_thumbnail( $post->ID ) ) {
                array_push( $showcase_invalid , $post->ID );
            } elseif ( !empty($redirect_url) && validateURL($redirect_url) !== 'good' ) {
                array_push( $showcase_invalid , $post->ID );
            }
        }
        set_transient( 'showcase_invalid', $showcase_invalid );
        wp_reset_postdata();
        return $showcase_invalid;
    }
    
    add_action("publish_post", "ts1_showcase_invalid_draft");
    add_action("save_post", "ts1_showcase_invalid_draft");
    add_action("publish_future_post", "ts1_showcase_invalid_draft");
    add_action("publish_phone", "ts1_showcase_invalid_draft");
    add_action("wp_insert_post", "ts1_showcase_invalid_draft");
    add_action("xmlrpc_publish_post", "ts1_showcase_invalid_draft");
    function ts1_showcase_invalid_draft() {
        $showcase_invalid_array = ts1_showcase_invalid();
        while (list($var,$value) = each ($showcase_invalid_array)) {
            $showcase_invalid = array();
            $showcase_invalid['ID'] = $value;
            $showcase_invalid['post_status'] = 'draft';
            wp_update_post( $showcase_invalid );
        }
    }

Topic Closed

This topic has been closed to new replies.

About this Topic