wp_insert_post two values into the post_title - php

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.

Related

multiple value insert using saveField function or other function in cakephp

I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);

Wordpress Database Values Wrong Format

i created a custom database for my wordpress installation. I am able to insert and get variables to/from it. My problem now is that the format of the variables is wrong.
// $pdflink is a url
$datum = date('d-m-y');
global $wpdb;
$tablename=$wpdb->prefix.'offers';
$wpdb->insert($tablename, array(
'benutzer' => $current_user_id,
'datum' => $datum,
'offer' => $pdflink,
),array('%d','%s','%s'));
The database looks like this.
$datum is "203" instead of "02.03.16" and $pdflink is "0" instead of a url.
What is wrong with it? Thanks for your help.

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

Column in SQL being deleted on UPDATE

I have a table of values that uses a Where clause to find the row and then updates the values in the row. However it is deleting the value it is searching for after it is already found. So it finds the row using the stockid (not a PK) and updates the row, but leaves the stockid blank afterwards
$data = array(
'towSet' => $towSet,
'transWare' => $transWare,
'oceanFreightBooked' => $oceanFreightBooked,
'BOLrec' => $BOLrec,
'BOLsent' => $BOLsent,
);
$this->db
->where('stockid', $stockHold)
->update('logistics_tracking', $data);
The Strange part is that using this code doing the same thing it works perfectly fine
$data = array(
'recTitle' => $recTitle,
'recPOA' => $recPOA,
'recTitleState' => $recTitleState,
'titleSent' => $titleSent,
);
$this->db
->where('stockid', $stockHold)
->update('title_tracking', $data);
So as a reminder all the other values (towSet, transWare, oceanFreightBooked, ect.. ) all insert and it just blanks out the stockid after updating
Any ideas would be greatly appreciated
Yeah the code was fine, apparently I was just doing too much with one function. Created an external function and passed through an array of the data. Weird.
Thanks for looking!

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.

Categories