WordPress post search with ignoring apostrophe - php

I am trying to search posts in wp_posts table
$searchValue = $request->get_param("s");
$wpQueryPosts = new \WP_Query([
'post_type' => 'post',
'posts_per_page' => -1,
'cat' => 9,
's' => $searchValue,
]);
$postsPosts = $wpQueryPosts->get_posts();
The problem is when I search word with apostrophe it's not searched properly. If I search don I get proper results, when I search don't I don't get any results. Even though I know there are posts with don't in their title and content. I found many similar questions but none of them has any clear answer.
Also how can I get same results for both dont and don't
the problem is that if word in database contains apostrophe, it doesn't appear in results

This should do the job:
$searchValue = html_entity_decode( $searchValue, ENT_QUOTES );

Related

Wordpress, how to make search for posts using get method?

So I am building a WordPress theme for myself to meet my needs, so what I am looking for now is make a search using a get method in php.
If my url is something like this :
http://www.MyWordPressWebsite.com/search.php?string=Bananas+and+apples
In my search.php I would like to do something like this:
string = $_GET["string"];
$fruits_args = array(
'post_type' => 'fruits',
'posts_per_page' => -1,
'cat' => 'fruits'
);
$fruits = new WP_Query($fruits_args);
So how do I making the loop related to the string that I get from the the get method ?
comparing the search string the the post type, category and post title would be enough.
use query_posts() instead and Wordpress will handle the search with those parameters. You can add as many parameters as you want.
Example:
<?php $my_post_type = (get_query_var('my_post_type')) ? get_query_var('my_post_type') : false;?>
<?php query_posts(array('post_type' => $my_post_type));?>
<?php //Normal loop here ?>
For more info visit https://codex.wordpress.org/Function_Reference/query_posts
I hope this help

wp_insert_post two values into the post_title

I'm currently working on inserting some data into my posts, I'm trying to insert two sets of data into the post title and I'm kinda stuck.
$my_post = array(
'post_title' => $data['title'],
This works fine, but I need to add another one to the title, how can I do that? for example
'post_title' => $data['title'] && $data['count'],
Any help would be appreciated, thank you.
Use a dot . to concatenate the titles:
'post_title' => $data['title'] . $data['count'],
See the PHP docs on string operators for more info.

Wordpress Multiple post from a post ID

I have a usermeta set up for my users that saves favorite post to their profile. I am getting this usemeta(which keeps the post IDs in it). Once I get it, I have it in an one dimensional array. I want to display a list of their favorite posts. I have tried this:
$favorites //array of favorites, that has come from the databese
$query = new WP_Query( array( 'post__in' => array( 2, 5, 12, 14, 20 ) ) );
and it would work fine, if I hard coded the post IDs , but since it is an array I can't just pass in array such, it returns nothing.
$query = new WP_Query( array( 'post__in' => $favorites) );
it does not accept it, I also tried to implode the array in to a string as such:
$fav_list = implode("," , $favorites);
and i get this, which is exactly what I need as a string
"124,126,125,130,132,140,142", without the quotes. I would then use it as such:
$query = new WP_Query( array( 'post__in' => array($fav_list) ) );
But again it doesn't work, it returns nothing. Since the favorites list is being pulled from the usermeta and the user can change it, I can't hard code the list.
Can anyone help me? Is it even possible with WP_Query. Not sure why it is not taking the string or what I am doing wrong. I red through the Wordpress Documentation but haven't found a solution.
Thanks in advance.
Arrays don't get stored in the database as arrays. They get serialized. When you pull the array from the database you have to unserialize() it.
http://php.net/manual/en/function.unserialize.php
If you var_dump($favorites) right after it comes out of the database, you'll notice it's a weird-lookin' string and not an array. var_dump(unserialize($favorites)) will show you your original array.

Numerical sort on custom field which includes letters

I'm trying to do a sort in my WP_Query on a custom field. The custom field contains strings such as "E100", "E500" and "E123b". I would like to sort numerically on these values, i.e. sort on the custom field as if the characters weren't there.
My query looks like this:
$subpages = new WP_Query(array(
"post_type" => "page",
"meta_key" => "[customFieldNameHere]",
"orderby" => "meta_value_num",
"order" => "ASC",
"posts_per_page" => 5000
));
But it doesn't work. It does some sort of sort, but it's not numerical. Is it possible to strip all characters/letters from the field, and then do a numerical sort on the remaining values, or is there some other way to solve this?
You can't sort a string with letters as if it were all numbers. If you try you will get a alphanumerical sort-- more or less what you'd do if you were alphabetizing something but it makes numbers look odd because it matches up all the first characters, then the second, and so on making numbers look like:
1
10
101
2
20
I don't know of anything built into MySQL that will do what you want, nor does WP_Query have any such feature. The best solution I have is to pull your results unsorted, and sort them later. You want to pull unsorted so the database doesn't do extra work.
$subpages = new WP_Query(array(
"post_type" => "page",
"meta_key" => "[customFieldNameHere]",
"posts_per_page" => 5000
));
$sorted = array();
foreach ($subpages as $s) {
$kname = preg_replace('/[^0-9]+/','',$s->customFieldNameHere]);
$sorted[$kname] = $s;
}
// you may need
// ksort($sorted);
var_dump($sorted);
One problem is going to be collisions. If you end up with two matching $knames you will clobber data, and frankly, that seems pretty likely if you have a lot of data.
I ended up removing the leading E from all strings. Doesn't really solve the original problem, with numerical sorting of strings, but at least it works now.

PHP & Array question

I have an array called $array_all;
This array will always have 3 possible values:
1,
1,2,
1,2,3,
I created a string from the array while in a foreach loop and concatenated a comma at the end.
So, now I have a nice string that outputs the exact value the way it should.
1,2,3, I can copy this output from my browser and insert it into my wordpress function and everything displays perfectly.
The problem arises when I insert this string variable in the wordpress function directly, it fails.
Anybody have any ideas?
Code below:
<?php
$faux_array = array();
$faux_array_all;
if($five_loans != ''):
$faux_array[] = "781";
endif;
if($logbook_loans != ''):
$faux_array[] = "797";
endif;
if($easy_money != ''):
$faux_array[] = "803";
endif;
foreach($faux_array as $faux_array_value):
$faux_array_all .= $faux_array_value . ',';
endforeach;
echo $faux_array_all;
$args = array
(
'posts_per_page' => 10,
'post_type' => 'lender',
'order' => 'ASC',
'orderby' => 'date',
'post__in' => array($faux_array_all)
);
?>
Mmh for one, you can avoid the loop with just:
$faux_array_all = implode(',', $faux_array);
which would also solve the trailing comma proble.m.
On the other hand, you pass an array to post__in that only contains one element (the string). I think what you really want is
'post__in' => $faux_array
as $faux_array is already an array with IDs.
Read about Post & Page Parameters, there you can see that you need to pass an array of post IDs to the function, not an array with one string value:
'post__in' => array(5,12,2,14,7) - inclusion, lets you specify the post IDs to retrieve
'post__in' => $faux_array
Try this, and if it doesn't work post the code that you manually make to work please.
Edited. Check it now.
You need to trim off the trailing comma
foreach($faux_array as $faux_array_value):
$faux_array_all .= $faux_array_value . ',';
endforeach;
if (substr($faux_array_all)-1,1) == ",") {
$faux_array_all = substr($faux_array_all,0,-1);
}

Categories