0

I'm trying to modify search results for my custom post type. I have clarified that the code I am using is returning users and post ID's but no results are displayed.

Essentially when a search is attempted via the edit-post screen for my custom post, I want the search to check all users by login name, email, nicename, and display name. A further check is then performed for each user found to see if their user ID exists in one of two custom meta keys for my custom post types. If it does, that post should be included in the results.

I am using the pre_get_posts hook with the following function...

function custom_post_query( $query )    {
        global $pagenow;

        if( !is_post_type_archive( 'my-custom-posttype' ) || !$query->is_main_query() || !$query->is_admin || 'edit.php' != $pagenow )
            return $query;

        if( $query->is_search() )   {
            $users = new WP_User_Query(
                array(
                    'search'            => $_GET['s'],
                    'search_columns'    => array(
                        'user_login',
                        'user_email',
                        'user_nicename',
                        'display_name'
                    )
                )
            ); // WP_User_Query

            if( !empty( $users->results ) ) {
                foreach( $users->results as $user ) {
                    $results = get_posts(
                        array(
                            'post_type'     => 'my-custom-posttype',
                            'post_status'   => 'any',
                            'meta_query'    => array(
                                'relation'  => 'OR',
                                array(
                                    'key'       => '_custom_meta_key1',
                                    'value'     => $user->ID,
                                    'compare'   => '==',
                                    'type'      => 'NUMERIC'
                                ),
                                array(
                                    'key'       => '_custom_meta_key2',
                                    'value'     => $user->ID,
                                    'compare'   => '==',
                                    'type'      => 'NUMERIC'
                                )
                            )
                        )
                    ); // get_posts

                    if( !empty( $results ) )    {
                        foreach( $results as $result )  {
                            $events[] = $result->ID;                                
                        }
                    }

                } // foreach( $users as $user )
            } // if( !empty( $users ) )

            if( !empty( $events ) ) {
                $query->set( 'post__in', $events );
            }
        } // if( $query->is_search() 

        return $query;
    } // custom_post_query
add_action( 'pre_get_posts', 'custom_post_query' );

EDITED - Adding resulting WP_Query SELECT SQL_CALC_FOUND_ROWS web_wp_posts.ID FROM web_wp_posts WHERE 1=1 AND web_wp_posts.ID IN (5818,5881,5717,5655,5636,5592) AND (((web_wp_posts.post_title LIKE '%*mike*%') OR (web_wp_posts.post_content LIKE '%*mike*%'))) AND web_wp_posts.post_type = 'mdjm-event' AND ((web_wp_posts.post_status = 'mdjm-unattended' OR web_wp_posts.post_status = 'mdjm-enquiry' OR web_wp_posts.post_status = 'mdjm-approved' OR web_wp_posts.post_status = 'mdjm-contract' OR web_wp_posts.post_status = 'mdjm-completed' OR web_wp_posts.post_status = 'mdjm-rejected' OR web_wp_posts.post_status = 'mdjm-failed')) ORDER BY web_wp_posts.post_title LIKE '%*mike*%' DESC, web_wp_posts.post_date DESC LIMIT 0, 20

Thanks

5
  • And what is the problem? Where does the code fail? What happens that shouldn't? Also, you don't need to return the query. The query object is passed by reference.
    – s_ha_dum
    Dec 10, 2015 at 13:25
  • Per my post... no results are displayed. The page loads fine, but states no posts. I was initially thinking that the post__in was the problem. As I stated, I have confirmed I have users and post ID's. It's just that no search results are displayed
    – Mike
    Dec 10, 2015 at 13:27
  • Are you sure there are posts matching all of the query requirements? In other words, you are setting post__in, but what does the rest of the query look like?
    – s_ha_dum
    Dec 10, 2015 at 13:55
  • OK, It starts as ... SELECT SQL_CALC_FOUND_ROWS web_wp_posts.ID FROM web_wp_posts WHERE 1=1 AND web_wp_posts.ID IN (5818,5881,5717,5655,5636,5592) which seems ok and confirms that my user lookup and get_posts are working, but after that there are a large number of WHERE clauses. I guess what I need to do is get rid of those by essentially removing the default WP_Query and replacing with my own. But I'm not sure of the best way to do this?
    – Mike
    Dec 10, 2015 at 16:46
  • Edit that entire generated query into the question please, so I can see exactly what is happening.
    – s_ha_dum
    Dec 10, 2015 at 16:48

0

Your Answer

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