• I’d like to transform the following code. This code was done to add a specific search for post ID numbers. Now I need a function to extend the search to a meta field called “function_name”, and the search is not public but from the admin post table

    function search_by_post_id($query) {
        if($query->is_search) {
            if(is_numeric($query->query_vars['s'])) {
                $query->set('post_type', 'any');
                $query->set('post__in', array((int)$query->query_vars['s']));
                $query->set('s', '');
            }
        }
        return $query;
    }
    add_filter('pre_get_posts', 'search_by_post_id');
    

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    What sort of logic to you want applied? ID AND function name must match? ID OR function name could match? These options would require two search fields since the data types are different. Or do you want to replace the ID search with function name search?

    Searching for function name involves setting the “meta_query” arg to that of an appropriate array structure, but how that is integrated into your current code varies by what sort of logic you want applied.

    Thread Starter sacconi

    (@sacconi)

    Probably the best solution for me is having an extra search box dedicated to the search of the function_name

    Moderator bcworkz

    (@bcworkz)

    So the two possible search term types involve completely independent searches? Because one term type is an integer and the other a string, you could stay with one field, then branch into one kind of search or the other based on term type. For example:

    if ( $query->is_search ) {
       if ( is_numeric( $query->query_vars['s'])) {
          $query->set('post_type', 'any');
          $query->set('post__in', array((int) $query->query_vars['s']));
       } else { // assume search term is a string
          // sanitize the search term
          // build an appropriate meta query arg array
          // set the 'meta_query' arg using the built array
       }
       $query->set('s', '');
    }

    For more on how to build a meta_query array, refer to the WP_Query doc page. Jump to the custom fields section from the table of contents. Scroll down a bit further to find examples using “meta_query”.

    BTW, pre_get_posts is an action, not a filter. Use add_action(), not add_filter(). The query object is passed by reference, so you’re directly setting values in its memory space. There’s no need to return a value from action hooks. Why does add_filter() work if it’s an action? The two are very closely related and behave very similarly. The difference is more semantic than practical. Maintaining clear semantics in code benefits future maintenance.

    Thread Starter sacconi

    (@sacconi)

    Thank you. At the moment I’m not able enough to set an entire complex function, just customize it

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Extending the search function to a specific meta field value in the admin posts’ is closed to new replies.