Custom Post Type, pre_get_posts and dynamic validation
-
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 🙂
- The topic ‘Custom Post Type, pre_get_posts and dynamic validation’ is closed to new replies.