0

During search in 2 meta key( say acf hr_archive_title and acf hr_archive_question ), I want to list down post in following order.

  1. first preference is post with searched text found in hr_archive_title
  2. then posts with searched text found in hr_archive_question

example data1: hr_archive_title - HR-fråga 1432 - Rekryteringsbonus; hr_archive_question - lorem ipsum

example data2: hr_archive_title - HR-fråga 1223 - Rekryteringskonsult i Norge - Rekryteringsbonus; hr_archive_question - Har det påverkat er rekryteringsprocess på något vis?

example data3: hr_archive_title - HR-fråga 1167 - Linkedin Recruiter; hr_archive_question - Har det påverkat er rekryteringsprocess på något vis?

Now when I search for 'Rekrytering' the listing should be as data1, data2, data3. Is this possible? Itried the following but to no avail. The listing is coming as mixed list.

Display results of one meta key and order by a different meta key

https://www.billerickson.net/wp-query-sort-by-meta/

https://wordpress.stackexchange.com/questions/218709/order-by-custom-field-value

Any help/suggestions are welcome.

samjhana joshi
  • 1,995
  • 4
  • 35
  • 69

1 Answers1

0

You can try something like:

$q = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        'title_clause' => array(
            'key' => 'hr_archive_title',
            'compare' => 'EXISTS',
        ),
        'question_clause' => array(
            'key' => 'hr_archive_question',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => 'title_clause'
) );

This will pull posts that have both meta keys set, but sort them by the hr_archive_title meta. meta_query is an array of meta clauses, each clause is an associative array with an identifier key. Then you set the orderby parameter to listen to the title_clause meta clause.

This exact query might not get you what you want, but play with the idea of "clauses" for your meta_query and you should be able to achieve what you are trying to do.

Idea taken from: https://wordpress.stackexchange.com/a/246358/70417

Ty Bailey
  • 2,392
  • 11
  • 46
  • 79
  • this is not what I want exactly. I want to search the word in either title OR question ( not in BOTH title and question ).So meta_query relation will be 'OR' in my case. Let's say when I search for specific word, the listing comes as follows. title1 title2 quetion1 title3 title4 But I like the listing as title1 title2 title3 title4 question1 How can I achieve this ? – samjhana joshi Jan 29 '19 at 04:50