Search outside of the wordpress “loop” - wordpress

I am creating a blog using only Wordpress's backend. I have found functions to get latest posts (wp_get_recent_posts) and all the required data I need. I do this by including wp-load so I have access to WP's functions.
However I cannot find anything that allows me to perform a search outside of Wordpress's theming loops as I have for the rest of the data.
I was hoping there was a search function where I can pass it a search query that could be in title, body content or tag name.
Am I missing something blindingly obvious in the documentation, there seems to be a function for everything else I need outside of WP's "loop".

Does that work for you?
// query for a given term ("search_term")
$search_query = new WP_Query();
$search_posts = $search_query->query('s=search_term');
Source

Answered by sanchothefat:
You can use get_posts() with a search parameter:
$results = get_posts( array( 's' => 'search term' ) );
https://wordpress.stackexchange.com/questions/74763/search-outside-of-the-loop/74766#74766

Related

Get post ID of the original version in WPML

I have a website using WooCommerce in the Slovenian language that is being translated with WPML into multiple languages and English will become the default one.
Products were all set in Slovenian language and use a custom field (ACF) for an image. The products are translated but the images are duplicated which causes issues in one case. The image should be of the original product.
I have now fixed this by using this:
$preview_img_id = get_field( 'preview_image', apply_filters( 'wpml_object_id', get_the_ID(), 'product', FALSE, 'sl' ) );
But this is not a good solution in the long term because new products might be added in another language first.
What function can I use to get the ID of the original post/product?
I'm not sure I understand what "causes issues in one case", but I believe you can omit the $ulanguage_code and set $return_original_if_missing to TRUE. Documentation:
$ulanguage_code - (mixed) (Optional) If missing or NULL, it will use
the current language. If set to a language code, it will return a
translation for that language code or the original if the translation
is missing and $return_original_if_missing is set to TRUE. Default is
NULL
If you always want to get the ID of the original product, then maybe you could trick it into doing this by specifying a non-existent language code, for example:
apply_filters( 'wpml_object_id', get_the_ID(), 'product', TRUE, 'xx' )

Wordpress get_post(); Only Returning 5 Entries

This has been driving me nuts trying to figure out: In my plugin I have a section that pulls all entries out of the Database - we'll say all posts, but I also display Pages and Categories - using this line of code:
$args_posts = array(
'numberposts' => -1,
'suppress_filters' => true
);
$posts = get_pages($args_posts);
foreach ( $pages as $page ) {
//formatting & display
}
...and then this get looped, formatted, and echoed using a foreach() statement.
The problem I'm having is that it runs fine on my server but when I have a friend try it it only shows 5 posts. The environment it needs to work under will end up having several thousand entries. Originally, the problem was there for me too but I didn't supply the arguments for get_posts() - I just set it as a variable itself using $posts = $get_pages(), and once I added the arguments it resolved. But my friend is still seeing only 5 entries displayed, even though there's no browser cache or server cache and other changes in the script are coming through.
Thank you in advance!
According to the Wordpress Docs:
Number
(integer) Sets the number of Pages to list. This causes the SQL LIMIT value to be defined. Default to no LIMIT. This parameter was
added with Version 2.8. Note: get_posts() uses the parameter
'numberposts' instead of 'number'. Second Note: it doesn't work if
used together with 'child_of'. Instead use 'parent' and set
'hierarchical' to false.
Link:
Documentation
In otherwords change numberposts to number

How bad is $wp_rewrite->flush_rules() on 'init' action hook?

According to the Codex:
[...] This function can be extremely costly in terms of performance. It should be used as sparingly as possible - such as during activation or deactivation of plugins or themes. Every attempt should be made to avoid using it in hooks that execute on each page load, such as init.
Ok, so, I know it shouldn't be used in every page load and I'm not, but I still need a very conditional rewrite rule on this project.
I have these 2 url structures:
- example.com/products/tables/fancy-computer-table //This is a product url, here I expect the normal behavior from wp.
- example.com/products/tables/office //This should be some kind of a filter, where the site gets all the tables related to the office department. Note how both URL structure matches.
To make it work, I narrowed it down to these very specific URLs, using some regex. If matched, I'll run 2 queries to verify if the url I'm in is for the filter I want that rule to apply, or if it's a product url. In the latter, I want wordpress to behave normally. But I have to flush the rules either way, whether it's a product or it's the category and the filter, so that both pages work properly and dinamically.
I do all this to narrow down the use of this function to the least possible, but the Codex doesn't really tell me how bad it does affect the performance, or why.
I'm passing the parameter on that function as false by the way, so it doesn't rewrite my htaccess file, but I'm not sure where that rewrite rule option is stored, if it's on the memory somewhere, or on the database, wish they would provide some clarification on that.
Thanks in advance for reading this question. Any pointers are appreciated here, even if you want to point me to some other way of doing this. :)
EDIT: Posting some code here so you guys can actually understand what I mean and let me know if this maybe this could be bad practice... maybe a suggestion on how to do it better.
<?php function custom_rewrite_products()
{
// regex to match either "products/tables/fancy-computer-table" or "products/tables/office"
preg_match('#products\/([^\/]+)\/([^\/]+)\/?#', $_SERVER['REQUEST_URI'], $url_matches);
if(is_array($url_matches)) {
$query_category = new WP_Query('category_name='.$url_matches[1]);
$query_category->get_posts();
$args = array(
'post_type' => 'filters',
'name' => $url_matches[2]
);
$query_post = new WP_Query($args);
$query_post->get_posts();
if($query_category->have_posts() && $query_post->have_posts()) {
$category_ID = '';
$filter = '';
$category_ID = '' . $query_category->query_vars['cat'] . '';
$filter = '' . $query_post->query_vars['name'] . '';
$string = "index.php?cat={$category_ID}&filter={$filter}";
add_rewrite_rule('products/([^/]+)/([^/]+)/?$', $string, 'top');
}
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules(false);
}
}
add_action('init', 'custom_rewrite_products');
I still don't understand why, but the flush is needed here, so the $category_ID and $filter variables actually get passed to the rewrite rules. If I take it out, the rewrite just goes to the index page, the place where the values should be are empty.
In case you're wondering, I created the filters on a custom post type and they are related to each post by using a custom field, since they're like four or five and they're present on every category on this project. As you can see, I already have a category/subcategory structure and I didn't find it smart to create these filters once again by hand inside each main category.
There is a 'rewrite_rules' record in the 'wp_options' table that includes all the rewrite rules. When calling flush_rules(), WordPress will clear this record and regenerate all the new rules, including the ones you're not changing.

Does query_posts have an effect on get_the_category?

I am having trouble debugging a situation, and I think some better background information on how these systems work would be very helpful. I know that the use of the function query_posts() is strongly discouraged. But let's just assume that there is nothing I can do to remove it from the code. Specifically the query is changed to pull posts of a post_type (a post_type specific to the theme I am using). This is what the query looks like after it has been changed:
posts_per_page=10&paged=0&post_type=project
Then the loop begins and it can successfully grab the titles for each post. But when I call get_the_category() it returns an empty array, even though all of the posts have categories. I verified that it was an empty array with var_dump.
I am do not have a super strong understanding of how these systems work, so the emphasis on not using query_posts has me worried. Is there any possible interaction between query_posts and get_the_category() that could cause it to not work correctly?
Why not using WP_Query()? Im not sure it is cause by query_posts, but query_posts() alters Wordpress Main Loop (global $wp_query).
You can use the WP_Query method like this, by replacing with your query_posts loop
$newLoop = new WP_Query('posts_per_page=10&paged=0&post_type=project');
if ( $newLoop->has_posts() ) {
while ( $newLoop->has_posts() ) { $newLoop->the_post();
the_title(); // Your title
var_dump ( get_the_category() ); // this should not be empty if category assigned to current post
}
}
If you still want to go with query_posts try one of the methods below:
Try passing post id manually:
get_the_category( get_the_ID() ); // Must be in loop
Or try adding this:
// after endwhile of query_posts
wp_reset_query();

reverse order of projects in wordpress

I have a list of latest projects displaying on the home page of my wordpress site. I can see the section that calls for the projects, but not sure where I can reverse the order.
I know this works for posts.
<?php query_posts($query_string . "&order=ASC"); ?>
but don't know where to add it. This is the code that calls the projects:
$wpGrade_Options->get('homepage_portfolio_limit') ? $projects_nr = $wpGrade_Options->get('homepage_portfolio_limit') : $projects_nr = 3;
wpgrade_display_portfolio( $projects_nr, true, true); ?>
You are probably using plugin or a theme.
You need to find this function in the plugin/theme files: wpgrade_display_portfolio
From command line you would simply do:
grep -ir "wpgrade_display_portfolio" wordpress/dir
Inside that function there is either a direct call to DB, or if writer of that script was wise built in wp get_posts, WP_POSTS, or query_posts functions.
IF you find any of these wordpress native functions than you can easily reverse order by adding:
'order' => 'DESC',
If there is a mysql query, then I will have to see it first to give you meaningful answer. I also need to see DB and how you actually would like to order things.

Resources