Support

Account

Home Forums ACF PRO add post type to search results and filter by meta value

Solving

add post type to search results and filter by meta value

  • Hello
    I’d like to add some custom post types to the WP search results – however, some of those post types have a status field and the search results should be filtered by that – obviously some other post types e.g. ‘post’ do not have that status field – how can I do that – is there an easy way:

    the following code is not working, or maybe works partially – hope somebody can get me going on this – thank you in advance for your help.

    ` function themeprefix_include_custom_post_types_in_search_results( $query ) {
    if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
    $query->set( ‘post_type’, array( ‘post’, ‘cpt1’, ‘cpt2’, ‘cpt3’, ‘opportunity’ ) );

    $query->set( ‘meta_query’, array(
    ‘relation’ => ‘OR’,
    array(
    ‘key’ => ‘cpt1_status’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’,
    ‘type’ => ‘NUMERIC’,
    ),
    array(
    ‘key’ => ‘cpt2_status’,
    ‘value’ => ‘1’,
    ‘compare’ => ‘=’,
    ‘type’ => ‘NUMERIC’,
    ),
    ));

    }
    }
    add_action( ‘pre_get_posts’, ‘themeprefix_include_custom_post_types_in_search_results’ );

  • You create a nested meta query

    
    $meta_query = array(
      'relation' => 'OR',
      array(
        // cpt1_status exists and is == 1
        'relation' => 'AND',
        array(
          'key' => 'cpt2_status',
          'compare' => 'EXISTS'
        ),
        array(
          'key' => 'cpt1_status',
          'value' => '1'
          'compare' => '='
        )
      ),
      // repeat to cpt2_status
    );
    
  • Thank you John – that somewhat got me going – what I did NOT considered/knew at all is that the custom fields are not searched at all so I’m not really sure where I will go from here.

  • Thanks David – what I did not knew/considered that the WP search does not search my custom fields – so we are able to add the post types to the search but not the custom fields it seems.

  • @John – when setting the meta query I’m not getting any search results of search terms in pages – how do I get around that?

  • I don’t know, I’d have to see your entire query. Please use code tags if you post it.

  • Here you go – I had to comment the meta query in order to have the default search returning any results?

       function themeprefix_include_custom_post_types_in_search_results( $query ) {
            
            if ( $query->is_main_query() && $query->is_search() && ! is_admin() ) {
                
                $query->set( 'post_type', array( 'post', 'case', 'client', 'event', 'job', 'opportunity', 'partner', 'team' ) );
                /*
                $query->set( 'meta_query', array (
                    'relation' => 'OR',
                    array (
                        'relation' => 'AND',
                        array (
                          'key'       => 'job_status',
                          'compare'   => 'EXISTS'
                        ),
                        array(
                            'key'     => 'job_status',
                            'value'   => '1',
                            'compare' => '=',
                            'type'    => 'NUMERIC'
                        ),
                    ),
                    array (
                        'relation' => 'AND',
                        array (
                          'key'       => 'opportunity_status',
                          'compare'   => 'EXISTS'
                        ),
                        array(
                            'key'     => 'opportunity_status',
                            'value'   => '1',
                            'compare' => '=',
                            'type'    => 'NUMERIC'
                        ),
                    )
                ) );
                */
            }
        }
        add_action( 'pre_get_posts', 'themeprefix_include_custom_post_types_in_search_results' );
  • You are going to need to add another section to the query to cover post types that do not have either of the fields.

    
    $query->set( 'meta_query', array (
                    'relation' => 'OR',
                    array (
                        'relation' => 'AND',
                        array (
                          'key'       => 'job_status',
                          'compare'   => 'EXISTS'
                        ),
                        array(
                            'key'     => 'job_status',
                            'value'   => '1',
                            'compare' => '=',
                            'type'    => 'NUMERIC'
                        ),
                    ),
                    array (
                        'relation' => 'AND',
                        array (
                          'key'       => 'opportunity_status',
                          'compare'   => 'EXISTS'
                        ),
                        array(
                            'key'     => 'opportunity_status',
                            'value'   => '1',
                            'compare' => '=',
                            'type'    => 'NUMERIC'
                        ),
                    ),
                    // custom fields do not exist
                    array(
                       'relation' => 'AND',
                        array (
                          'key'       => 'job_status',
                          'compare'   => 'NOT EXISTS'
                        ),
                        array (
                          'key'       => 'opportunity_status',
                          'compare'   => 'NOT EXISTS'
                        ),
      
                      )
                ) );
    
  • Thank you John – very much appreciated – I still have to learn a lot.

  • getting a server error now – maybe just timing out:

    Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.

    More information about this error may be available in the server error log.

  • Sorry for the late reply, yes, too many nested queries can cause the query to time out.

    You have a complex search. I have done similar things.

    You could simplify the search by doing some more work. You want to exclude some fields from search.

    I would set up a standard WP meta field “exclude_from_search”

    When one of your custom post types is updated add an acf/save_post filter. In this filter determine by your custom fields if the post should be excluded and update post meta “excluded_from_search” to 0 or 1 (false or true).

    The you can simplify the query to look for this field not existing or set to 0 rather that have multiple meta values that need to be looked at.

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

You must be logged in to reply to this topic.