Sorting your posts by relevance. The code below will output the posts ordered by relevance I have added some comments for some clarification let me know if it works.
function my_custom_query(){
// get only the posts without order them
$my_query_posts = new WP_Query( array( 'numberposts' => 5 ) );
// check relevance posts
foreach( $my_query_posts->posts as $post )
$post->relevance = check_relevance( $post );
// sorting the posts
usort( $my_query_posts->posts, 'compare' );
return $my_query_posts;
}
function check_relevance( $post ){
// check and calculate the relevance post
return rand( 0, 100 );
}
function my_compare( $a, $b ){
if( $a->relevance == $b->relevance )
return 0;
return ( $a->relevance > $b->relevance ) ? -1 : 1;
}
$posts = my_custom_query();
// output posts
while( $posts->have_posts() ){
$posts->the_post();
}
Related Posts:
- Search – Ajax – Alter Query Parameters with Pagination
- WP_Query orderby breaks when using AJAX?
- How to filter a static post page with ajax and $wp_query
- Filter admin ajax data by url query
- WordPress WP_Query offset parameter not working with search parameter
- AJAX multiple search boxes not merging with array merge
- WP_Query orderby post__in remains ineffective in the Loop [closed]
- AJAX search on post pages by custom post type
- ajax category filter
- How to order posts tag by tag?
- Want to redirect if search query match exact title of any post
- Ajax and WP_Query/tax_query parameter
- Order posts by date and then by custom field
- How to order a post type with meta_value_num and if meta_value_num does not exist then order by date
- Pagination wont work with search results template [duplicate]
- ‘posts_where’ filter not applying ‘WP_Query’ in `wp_ajax`
- WP_Query class not found
- How to order posts in wp_query by a meta_value of the corresponding author
- Order by menu structure
- WP_Query order by date in meta_value
- Show all parents and children in custom post type in right order
- Searching through different categories on different pages code is not working
- Update Loop with Form
- Meta query with order by another custom field
- WP_Query’s “request” SQL Query
- Refine/Filter WP_Query Posts with jQuery or Ajax
- Show selected images on top in Media Manager
- is_search was called incorrectly
- How do I search inside specific taxonomies in WordPress
- How can I order Wp_Query hierarchically?
- How do I sort posts with multiple pages
- WP_Query ajax loader detect end of posts
- Complex date range with WP_Query and BETWEEN
- How to loop for every result found in the_content() when using the search query?
- showing all search result in one template
- Ajax Pagination on Ajax filter
- Wp_query returning only one post while query through ajax
- SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
- Order By table field comment_status in WordPress > 4.0
- AJAX search posts and pages
- How to orderby multiple meta fields if some fields are empty
- Multiple Search Terms WP_Query
- WP Query Args – Title or Taxonomy Value
- Ajax WP_Query reutrns no results on author.php
- WordPress pagination not working on template search.php
- wordpress search word, “hello world” ===> ‘hello+world’ ===> ‘hello’, ‘world’
- Order ascending is ignored in meta query?
- Categorising search results based on Custom Fields
- How to filter a query by multiple meta keys and order by other meta keys
- Multiple Orderby’s using random order WP_Query
- using pre_get_posts for search results not found
- Search posts missing a particular custom field
- Ajax future single post query doesn’t work when NOT logged in
- Debugging wp_query orderby for taxonomy
- Advanced WP_Query with meta_query, orderby?
- Get a list of posts by specific category
- Redirect Search to Form When No Product Results Are Found
- Issues with search after added meta_query
- How to search for meta_query LIKE or tax_query LIKE and grab these posts on search results?
- Retrieve data from wordpress db via input and put those result in dropdown
- WP_Query with multiple orderby NOT working with ASC and DESC, what’s wrong?
- Filtering by multiple conditions in the loop
- I need to run a AJAX Fuction from within a WP_Query but only works on first item
- Randomly select one of last 3 posts
- orderby ignored by wp_query
- $wp_query->found_posts not returning correct value
- How to search by slug and old slug in single API
- WP_Query orderby
- What’s wrong with this meta query? (order by meta key, then title, doesn’t work)
- Using Ajax and WP_Query to load more posts on category page fails
- Search Exact on WordPress – Relevannsi Solution Not Working
- Pagination repeating posts on search results
- I have problems with the search query using multiple post types
- Categories In English version showing not canonical URL, instead shows query search result
- WP_Query random ordering not working since 5.4
- Order by meta date with a thousanth of a second defaults to post order
- How to debug none working search query?
- WP Query. Ordering posts by another post types meta
- Best way to query posts and order by relevancy to query
- Include search tags and users in my search results system
- How to order posts, that have already been filtered by custom taxonomy, by their category names?
- A very strange problem with search query
- Display three sequential posts on each page load, without repeating previous
- how to avoid reloading/refresh the page when displaying the post of wp_list_categories
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Search Functionality broken by the wp 4.2 update
- Custom search (wp query by custom fields)
- Custom Search Query – include only custom fields and title
- Search Query that Includes Custom Table
- Order by multiple meta keys on wordpress
- How return normal search result using wp_ajax action for wordpress
- WP_Query with several meta_query-statements and order by meta_value
- Why does WP_Query not search for two ‘meta_query’ keys separated with OR?
- Get posts that were most recently tagged
- Refresh wp_query with no changes
- WordPress WP_Query Search (‘s’) With Multiple Search Terms
- Search results stuck on page 1
- Custom Taxonomy in custom REST API search
- How to put posts with some taxonomy on top of others in `pre_get_posts`
- WP_Query sort by ACF date field (newest first) with blank dates first