I would like to add/inject/append extra results into the WordPress search results.
At the moment WordPress only allows you to "adjust" the query that is being executed on its own database, but doesn't allow you to modify (or in WordPress lingo, filter) the results array.
I.e: If in WordPress I search for the term 'potato' all posts related to this term come back. I want to include results that I've obtained via a different service into the WordPress results set.
Just to clarify, I'm getting my results from a 3rd party API call. Not from the WordPress database.
Does anyone have an idea on how I can do this?
Edit: Preferably this needs to happen in my WordPress plugin without having to change search templates.
You can use pre_get_posts to add/edit your search result without changing things is search template.
To exclude pages
To exclude pages in your search results. It is possible to create an action hook that limits the search results by showing only results from posts.
The following example demonstrates how to do that:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
}
}
add_action('pre_get_posts','search_filter');
Include Custom Post Types in Search Results
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('post_type', array( 'post', 'movie' ) );
}
}
}
add_action('pre_get_posts','search_filter');
Include Custom / API Results
function search_filter() {
if ( is_search() ) {
// Do your API call here
// Save retrieved data in your wordpress
// This will also help to you avoid api call for repeated queries.
$post_id = wp_insert_post( $post, $wp_error ); // wp_insert_post() // Programatically insert queries result into your wordpress database
array_push( $query->api, $post_id );
}
}
add_action('pre_get_posts','search_filter');
function have_posts_override(){
if ( is_search() ) {
global $wp_query;
$api_post = $wp_query->api;
foreach ($api_post as $key) {
// This will enable you to add results you received using API call
// into default search results.
array_push($wp_query->posts, $key);
}
}
}
add_action( 'found_posts', 'have_posts_override' );
Reference:
Exclude_Pages_from_Search_Results
Include_Custom_Post_Types_in_Search_Results
The question might be old, but I had a similar problem and someone in the future maybe as well, so I'll post my (better) solution.
As said above, it would have been the cleanest solution to save the api query results in the wp_posts-table (as a custom post type maybe) and perform a normal WP-Search on them.
But in my case I had to deal with another table which cached some external data and link it to IDs of existing posts and I could not change this structure.
Therefore the Solution of Abhineet Verma (search_filter + pre_get_posts) seemed useful at first, but in the end it was not suitable for me. I was able to add additional posts to the query result, but they don't fit into the normal pagination of the results. For example, If you want to display 10 results per page and use the solution above and get twenty results from the api, you would have 30 results per page: the ten normal results, which belong to the page plus all twenty on every page. It also breaks some pagination-plugins.
So I finally solved the problem as follows. I used the filter posts_search to extend the sql-query:
add_filter('posts_search', function($sql) {
if (!$sql) {
return $sql;
}
global $wpdb;
$sqst = "select id from {$wpdb->prefix}mytable ... bla bla -> query to my custom table, wich return a bunch of IDs";
$sqlr = "AND (({$wpdb->prefix}posts.ID in ($sqst)) or (1 = 1 $sql))";
return $sqlr;
});
It looks a little bit strange but works very good. Maybe it will help someone sometimes!
Related
I am new to php, please help.
I am using this shortcode to show total number of published posts (400), it works fine. If I change ('post') to ('page') it shows total number of published pages (100) just as fine. I am trying to show total number of posts and pages (500) using one shortcode. Is it possible to add the two results and display as one total result?
function published_posts($atts) {
return wp_count_posts('post')->publish;
}
add_shortcode('posts_count', 'published_posts');
Yes, what you're attempting to do is possible. Simply assign the value of wp_count_posts() for both posts and pages to variables and then add them together in the return statement.
Example:
function published_posts( $atts ) {
$post_count = wp_count_posts( 'post' )->publish;
$page_count = wp_count_posts( 'page' )->publish;
return $post_count + $page_count;
}
add_shortcode( 'posts_count', 'published_posts' );
While this will work, I'm not convinced it's the most efficient approach you could take. It's making two trips to the database for information which could be retrieved in one. That being said, wp_count_posts() won't accept an array of post types so you'd likely have to code something from scratch.
I'm trying to integrate a Query Expansion software with Wordpress, but the wordpress search engine is an AND searcher (returns those contents that contain all the words in your query, and therefore the more extensive is the query the fewer results returns).
For that reason when i use the query with the expansion features, the Searcher returns me less results.
I would like to modify the Search's behavior, and turn it into something more like an OR searcher.
Example:
For the query " iot, internet of things", I would want those posts which contents "iot", or "internet of things, or both of them.
I've reading through this document (https://codex.wordpress.org/Class_Reference/WP_Query) and i tried to use something like this:
add_action( 'template_redirect', 'hooks_setup' , 20 );
function hooks_setup() {
if (! is_home() ) //<= you can also use any conditional tag here
return;
add_action( '__before_loop' , 'set_my_query' );
add_action( '__after_loop' , 'set_my_query', 100 );
}
function set_my_query() {
global $wp_query, $wp_the_query;
switch ( current_filter() ) {
case '__before_loop':
//replace the current query by a custom query
//Note : the initial query is stored in another global named $wp_the_query
$wp_query = new WP_Query( array(
'post_type' => 'post',
'post_status' => 'publish',
//others parameters...
) );
break;
default:
//back to the initial WP query stored in $wp_the_query
$wp_query = $wp_the_query;
break;
}
}
But I don't know which parameters i need to set in wp_query. How can I achieve this?
To answer what parameters you need to set, you have to read through the same page you mentioned. Parameters for various items (post types or categories) are defined on this page and when you want to change it all you have to do is modify the parameters.
If you dont know what query you want to do I think its hard for people to help. If you are getting less results, you can open query a bit further or obtain results in 2 queries. Read through the page and you will find its common when people want to obtain different types of data and not everything is in the loop.
Also read through this to understand more about the loop and how to reset the query and do a new one then you can obtain two result sets and use them or combine them when and where you want.
Just to give you an example for string search in posts with s as parameter from the same page Prepending a term with a hyphen will exclude posts matching that term. Eg, 'pillow -sofa' will return posts containing 'pillow' but not 'sofa' (available since Version 4.4).
This is my first post here, so I apologize in advance for any mishaps.
I've been searching for hours trying to figure this one out, but I simply can't seem to understand why this is happening.
The site I'm setting up is a child site (not in a multisite sense, but as a separate site/domain with the same branding). Some of the posts on my site will originate from the parent/main site (but will be made as new posts through copy-paste), and I want the original article ID as part of the permalinks.
E.g. http://www.example.com/hello-world/12345/, where 12345 is the article ID of the article on the parent/main site.
To accomplish this, I've added a custom field to my posts where I can add the article ID of the original article with external_article_id as Field Name. I've then tried to manipulate the permalinks with the following code:
add_filter('post_link', 'append_custom_permalink', 10, 2);
function append_custom_permalink($url, $post) {
$newurl = $url;
if ($post->post_type == 'post') {
$custom = get_post_custom_values('external_article_id', $post->ID);
if (!empty($custom))
$newurl = $url . $custom[0] . '/';
}
return $newurl;
}
Whenever I output the permalink to the posts it appears exactly as I want it, both in the editor and on the site. However, when I either click a link or enter the address manually, I get redirected automatically to http://www.example.com/hello-world/12345/12345/. It duplicates the additional numerical slug, and also happens when I replace $custom[0] with a hard-coded numeric value. This applies to all posts, and my permalink structure (in the settings) is set to /%postname%/.
I even tried setting the permalink structure to /%postname%/%ext_article_id%/ and replace %ext_article_id% with $custom[0], but with the exact same outcome. I also tried using the same code on another WordPress site, except this time with pages instead of posts, also with the exact same outcome.
Ideally I would like to use something like add_query_arg($custom[0], '', get_permalink($post->ID));, but omit the question mark that comes along with it.
Could someone please explain to me why this is happening, and how I can circumvent this? Do I need to use some other filter, or how can I approach this?
Thank you in advance!
In order to make this work you also need to make WordPress aware of the rewrite_tag and specify an additional permalink structure via add_permastruct. The following code should do the trick:
function append_custom_permalink( $post_link, $post ) {
if( $post->post_type == 'post' ) {
$custom = get_post_custom_values( 'external_article_id', $post->ID );
if( ! empty($custom) ) {
$post_link = $post_link.$custom[0].'/';
}
}
return $post_link;
}
add_filter('post_link', 'append_custom_permalink', 10, 2);
function sof_30058470_posts_rewrite() {
add_rewrite_tag('%external_article_id%', '([^/]+)', 'external_article_id=');
add_permastruct('external_article_id', '/%year%/%monthnum%/%day%/%postname%/%external_article_id%/', false);
}
add_action('init', 'sof_30058470_posts_rewrite', 10, 0);
Make sure to re-save your permalink structure at Settings->Permalinks once you added the code. You may also need to refresh/clear your browser cache.
I have SearchWP included on my site. It works fine, with one exception: Supplemental Search Engine for Custom Post Type. I have implemented search engine for standard posts post type, which are my News with Setting up a Supplemental Search Engine: Step by Step tutorial.
Code in template that returns my results for News is:
global $post;
// retrieve our search query if applicable
$query = isset( $_REQUEST['swpquerypub'] ) ? sanitize_text_field( $_REQUEST['swpquerypub'] ) : '';
if( class_exists( 'SearchWP' ) ) {
$engine = SearchWP::instance();
$supplementalSearchEngineName = 'wyszukiwanie_publikacji';
$posts = $engine->search( $supplementalSearchEngineName, $query, $swppg );
}
and it works perfect.
But if I do same thing with my post type "publication" in archive-publication.php, I'm not getting results except one: my current page object. I think my problem is global $post call in custom post type loop. Anyway, as far as I know, I can call global $post inside loop of current post type, and it will return my post type objects, but it seems to work different. I'm trying to solve this for long time, and for now, I didnt find solution, so I'm asking you for help.
I have created two plugins. Plugin A gets the latest posts, all of them. It has an option that can remove posts by entering their ID, this is accessible via the shortcode or directly in functions.php. This works.
Plugin B gets the most popular posts using an internal analytics systems. I have a variable called $most_popular_ids which contains all of the post ID's of the highest viewed posts for that particular category. So $most_popular_ids changes depending on which page it is on. This works.
These are displayed side by side on the main pages of the website. So ideally I would like them to not show duplicate posts.
What I finally need to do is pass $most_popular_ids from plugin B to plugin A or to functions.php. This will allow me to exclude all of the most popular posts from the latest posts.
Obviously making the variable global only works in the scope of the file so that won't work. I tried creating a $_SESSION but you can't do that in Wordpress as far as I know. Most of the stuff in the plugins can't be redeclared so my include attempts didn't work either :\
Can anyone help me?
You can use sessions in WordPress, but it would probably be easier to use the wp_cache_set() and wp_cache_get() functions
For example, in Plugin B you would do something like:
wp_cache_set('mykey_most_popular_ids', $most_popular_ids);
Then in Plugin A or your functions file you can do:
$most_popular_ids = wp_cache_get('mykey_most_popular_ids);
Obviously you'd need to make sure the function in Plugin B sets the cache entry before trying to get it in Plugin A of the functions file
Here's an approach using filters. In plugin A, implement a filter that allows to set the post__not_in param of the query:
$query_args = array();
$post__not_in = apply_filters( 'check_for_most_popular_ids', false );
if( $post__not_in && is_array( $post__not_in ) )
$query_args['post__not_in'] = $post__not_in;
$query = new WP_Query( $query_args );
In plugin B, hook into the filter and pass the most popular IDs:
add_filter( 'check_for_most_popular_ids', 'send_most_popular_ids', 10, 1 );
function send_most_popular_ids( $post__not_in = false )
{
$most_popular_ids = get_most_popular_ids(); // returns array of IDs OR false
if( $most_popular_ids )
return $most_popular_ids;
return $post__not_in;
}