0

I am trying to filter a CPT by an array of post ids using pre_get_post filter within my plugin. Everything works fine when there are post that match the ids, but when there are no post that match the ids I am getting all the post within the CPT that I am trying to filter in return. What I need id there are not post ids found (or post from the post ids) is the archive page to display the normal 'no results found'.

This is a run down of my script...

add_action('pre_get_posts', array(__CLASS__, 'get_posts')); //run inside a class
function get_posts($wp_query) {

        if ($wp_query->is_main_query() && !is_admin() && is_post_type_archive(MbeEnrollments::MBE_POST_TYPE_NAME) && $wp_query->query_vars['list-view'] == 'list-view') {

      //For the purpose of this sample I removed the script in the section that produces the CPT post ids that I am trying to return. This portion works fine and as expected.

            //if post ids are found 
            if ($post_ids) {

                $wp_query->set('post__in', $post_ids);

            }
            return;


        }


    }

I assume the issue is that the CPT archive page will run as normal if my if($post_ids) clause return false. However, I am trying to find a way to make it return with no results. Thanks in advance for any help.

UPDATE: If I include a post id that I know does not exist within the CPT I get the desired 'No Results Found'. Even though this works it seems a little bit like a hack and there has to be a cleaner way. Any ideas? Here is the if that works....

            if ($post_ids) { 

                $wp_query->set('post__in', $post_ids);

            }else{//if no post ids then pass in a fake post id to get the 'no results found'

                $wp_query->set('post__in', array(0));

            }
            return;
5
  • Your solution seems fine to me but you should post it as an answer not as an edit to the question. I assume that "the section that produces the CPT post ids" runs another query? Why can't a single query do this?
    – s_ha_dum
    Nov 26, 2013 at 14:37
  • I was not sure it was a true answer to my question. Yes, to get the post ids I run a complex query as seen in this post wordpress.stackexchange.com/questions/123630/…. I then need to loop those results with other factors to find the right results. Much too complex to combine into a single query. Thanks for your help. Nov 26, 2013 at 16:07
  • Well, unless you feed the query something that fails, it will return results and not the 404 that you want. There are other ways to do it but this way seems fine to me.
    – s_ha_dum
    Nov 26, 2013 at 16:09
  • What return do you get for $post_ids if there are no post IDs that match? An empty array? false? null? Something else? Nov 26, 2013 at 16:58
  • It returns empty if no post ids Nov 26, 2013 at 17:24

2 Answers 2

0

Assuming that you get an empty array for $post_ids if there are no matching posts, I think this is wrong:

if ( $post_ids ) {}

Because the array exists, but is empty. Try to check for an empty array:

if ( ! empty( $post_ids ) ) {}
1
  • Thanks Chip...each way works but why do you suggest the !empty() approach? Nov 26, 2013 at 17:26
0
$query->set( 'post__in', $post_ids );

This line is responsible to apply filters on the query.

But you are using it ONLY when $post_ids is true. that means that when $post_ids evaluates to false, then you are not filtering.

Basically, you are saying:

if ($post_ids) { //if there are values to filter

                $wp_query->set('post__in', $post_ids); // then use them to filter

            } // otherwise don't filter.
            return;

So just make sure that $post_ids is always an array, even an empty one. Then don't conditionnally set the query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.