0

I have the below code which filters search results to only return those of a specific parent page:

function SearchFilter($query) {
if ($query->is_search) {
    //$query->set('post_type', 'page');
    global $post;
    $query->set( 'post_parent', '1548' );
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

What I am looking to do instead of return the entire list of posts, is to return only the line in which the results were found, and display those in an unordered list.

So, say I search for "nott" It would not list all the posts which contain "nott", but instead return the lines, eg:

  • Nottingham
  • Notting Hill
  • Nottoway

Any idea how that might be possible?

2 Answers 2

0

Well you can try to do while(have_posts()) - a standard query loop in your search page. Than you can try to highlight (in example its marking a search occurance of the search string), but you can modify example and show only your search word occurance.

function sublinhamos($text, $words) {
    $wordsArray = array();
    $markedWords = array();
    // explode the phrase in words
    $wordsArray = explode(' ', $words); 

    foreach ($wordsArray as $k => $word) {
      $markedWords[$k]='<mark>'.$word.'</mark>';
    }

    $text = str_ireplace($wordsArray, $markedWords, $text);

    //right trows results
    return $text;
}

source of code

0

This is a little tricky, because "line" is far from simple complex.

  • Is it a plain text line?
  • Is it a HTML paragraph?
  • Is it a line of a paragraph?
  • What if it matches more than once in line?

It would be impractical to try express this in SQL and integrate with query results.

For these requirements I would just manipulate full content returned inside the loop.

1
  • It would be a single line within a paragraph. Something like: <p>Nottingham<br> Notting Hill<br> Nottoway</p>
    – Ian
    Jul 9, 2014 at 10:02

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.