how to use wp_update_post with multiple posts ids - date

please i have this code which set the date for the post id 144600, but please need to add 2 id's to be in the code these ids 456 and 874 , please how to add them to the code
$id = 144600;
$date_time = "2022-10-31 23:59:59";//[Y-m-d H:i:s]
wp_update_post(
array (
'ID' => $id,
'post_date' => $date_time,
'post_date_gmt' => get_gmt_from_date( $date_time )
)
);
please i have this code which set the date for the post id 144600, but please need to add 2 id's to be in the code these ids 456 and 874 , please how to add them to the code

Related

Cannot retrieve data from find query

In my Event model, I have the following function to retrieve all the events with status = 1 with a 12 limit and order according to event created DESC:
public function latestEvents() {
$this->Behaviors->load('Containable');
$result = $this->find('all' ,array('recursive' => -1, 'conditions'=> array('Event.status' => 1), 'limit' => 12, 'order' => array('Event.created DESC')));
debug($result); die();
return $result;
}
This function is not returning any data. When I change my limit to 6 and debug it returns six records but when I change my limit to more than 6 it returns (empty) this :
I even checked in my database by doing this query :
SELECT * FROM `events` WHERE `status` = 1 ORDER BY `created` DESC LIMIT 12
and this returns the desired data that I want. I even tried :
$result = $this->query('SELECT * FROM `events` WHERE `status` = 1 ORDER BY `created` DESC LIMIT 12');
but the same thing is happening with the limit (6 returns the data but more than 6 does not).
I found out that the data I was trying to debug had special characters and I had to include 'encoding' => 'utf8' in my database.php and worked like a charm. This post helped me.

Cakephp 3 find('list')

I wanted to prefill a multiple Checkbox form with Data from MySql Database.
My Database Data :
id customer_id language_id preferred
1 10 150 yes
2 10 149 yes
The query in Controller "Preferredcustomerlanguages"
$clientlanguages = $this->Preferredcustomerlanguages
->find('list')
->select(['language_id'])
->where(['customer_id =' => $customer_id])
->where(['preferred =' => 'yes'])
->toArray()
;
If i start the query with $customer_id = 10 the result is this:
[
(int) 2 => (int) 2,
(int) 1 => (int) 1
]
I thought the query would give me a list of the language_id which i need to prefill my form.
Maybe someone can give me a hint where iḿ thinking wrong
If you want to select a field different to displayField, you can use the list arguments for this:
$clientlanguages = $this->Preferredcustomerlanguages
->find('list', [
'keyField' => 'id',
'valueField' => 'language_id'
])
->where(['customer_id =' => $customer_id])
->where(['preferred =' => 'yes'])
->toArray();
https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

Additional information in one API call

Given below my code outputs number of Posts, Likes, Comments, and Shares for my Facebook page. It considers only present one week data. This is done using fql qpproach (I know fql is deprecated but my code works fine because the app I am using is an older app). I have few questions to ask and need help in getting that done:
I need help in converting this code using the latest api approach so that it can work on newly created apps.
The code outputs total number of likes, comments and shares just fine but is it possible to get the usernames of those who liked, commented and share the posts of my page in the past one week and update the values in a database table? If yes, how?
If point #2 is possible, can this be done in one API call?
Appreciate some assistance here.
<?php
require('config.php');
session_start();
?>
<?php
$facebook = new Facebook(array(
'appId' => '012myappid210',
'secret' => 'abc012myappsecret210cba',
'cookie' => true,
));
$token=$_SESSION['token'];
$pageid='88978302070'; //(my facebook page id)
$d=strtotime("now");
$d2=strtotime('now - 7 days');
$fqlAPIParams = array(
'method' => 'fql.query',
'query' =>'
SELECT post_id,comments,message,likes,created_time,share_count
FROM stream
WHERE actor_id = '.$pageid.' AND
source_id = '.$pageid.' AND created_time <= '.$d.' AND created_time >= '.$d2.'
LIMIT 250' ,
'access_token'=>$token
);
$result = $facebook->api($fqlAPIParams);
$postCount = 0;
$likescount=0;
$commentscount=0;
$sharescount=0;
foreach($result as $post)
{
$shares=$post['share_count'];
$likes=$post['likes']['count'];
$comments=$post['comments']['count'];
$likescount+=$likes;
$commentscount+=$comments;
$sharescount+=$shares;
$postCount++;
}
echo "Post " . $postCount . " Likes " . $likescount . " Comments " . $commentscount . " Shares " . $sharescount;
?>
Here is the output of the code you suggested:
Post: Likes: 25 Comments: 5 Shares: 30
Post: Likes: 0 Comments: 0 Shares:
Post: Likes: 25 Comments: 25 Shares: 54
PageID 88978302070 has 2 posts in the past 30 days (on 31st Oct and 21st Oct)
Few changes what I made:
1. I changed 'until' to 'since' 88978302070/posts?fields=likes,comments,shares&since=-30 days.
2. I changed the inner loop because one more issue I found was... instead of $post['likes'] and $post['comments'], it should have been $post['likes']['data'] and $post['comments']['data']
Code is working but Now the problem is:
1. It lists three posts whereas the page has only 2. One additional post is shown with 0 likes, 0 shares, 0 comments. Not sure where it is coming from.
2. Like count is incorrect. It only displays and lists a maximum of 25 likes and comments. I tried to put a limit of 999999 but it displays maximum 1000. Is there any solution to this? Actual like count for the two posts are 24483
3. Comment count is incorrect. It has to be 90 but the code lists 25+5=30
4. It does not lists usernames of those who shared the posts.
Without using FQL, you can do the following:
$d2 = strtotime( '- 7 days' );
$result = $facebook->api( $page_id . '/posts?fields=likes,comments,shares&until=' . $d2 );
foreach ( $result['data'] as $post ) {
echo 'Post: ' . $post['id'] . ' Likes: ' . count( $post['likes']['data'] ) . ' Comments: ' . count( $post['comments']['data'] ) . ' Shares: ' . $post['shares']['count'] . '<br/>';
}
The above API result also includes the name and IDs for all the users who have liked / commented on each post. You can do another foreach loop in the code to cycle through the likes / comments and update your database accordingly.
Example:
// main loop to cycle through posts
foreach ( $result['data'] as $post ) {
// inner loop to cycle through likes
foreach ( $post['likes'] as $like ) {
echo 'User: ' . $like['id'] . ' Name: ' . $like['name'];
// code to check / update db here
}
}
Running the above code for page 177526890164 (Narendra Modi) using limit=2 returns the following. If you are seeing a blank page or an error, you're clearly doing something wrong:
The best way to test would be to make the API call, and then add print_r( $result ) after to see if Facebook returns any data. If it's empty, there is something wrong with your API call.

Additional conditions in JOIN

I have tables with articles and users, both have many-to-many mapping to third table - reads.
What I am trying to do here is to get all unread articles for particular user ( user_id not present in table reads ).
My query is getting all articles but those read are marked, which if fine as I can filter them out (user_id field contains id of user in question).
I have an SQL query like this:
SELECT articles.id, reads.user_id
FROM articles
LEFT JOIN
reads
ON articles.id = reads.article_id AND reads.user_id = 9
ORDER BY articles.last_update DESC LIMIT 5;
Which yields following:
articles.id | reads.user_id
-------------------+-----------------
57125839 | 9
57065456 |
56945065 |
56945066 |
56763090 |
(5 rows)
This is fine. This is what I want.
I'd like to get same result in Catalyst using my article model, but I cannot find any option to add conditions to a JOIN clause.
Do you know any way how to add AND X = Y to DBIx JOIN?
I know this can be done with custom resoult source and virtual view, but I have some other queries that could benefit from it and I'd like to avoid creating virtual view for each of them.
Thanks,
Canto
I don't even know what Catalyst is but I can hack the SQL query:
select articles.id, reads.user_id
from
articles
left join
(
select *
from reads
where user_id = 9
) reads on articles.id = reads.article_id
order by articles.last_update desc
limit 5;
I got an solution.
It's not straight forward, but it's better than virtual view.
http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Relationship/Base.pm#condition
Above describes how to use conditions in JOIN clause.
However, my case needs an variable in those conditions, which is not available by default in model.
So getting around a bit of model concept and introducing variable to it, we have the following.
In model file
our $USER_ID;
__PACKAGE__->has_many(
pindols => "My::MyDB::Result::Read",
sub {
my $args = shift;
die "no user_id specified!" unless $USER_ID;
return ({
"$args->{self_alias}.id" => { -ident => "$args->{foreign_alias}.article_id" },
"$args->{foreign_alias}.user_id" => { -ident => $USER_ID },
});
}
);
in controller
$My::MyDB::Result::Article::USER_ID = $c->user->id;
$articles = $channel->search(
{ "pindols.user_id" => undef } ,
{
page => int($page),
rows => 20,
order_by => 'last_update DESC',
prefetch => "pindols"
}
);
Will fetch all unread articles and yield following SQL.
SELECT me.id, me.url, me.title, me.content, me.last_update, me.author, me.thumbnail, pindols.article_id, pindols.user_id FROM (SELECT me.id, me.url, me.title, me.content, me.last_update, me.author, me.thumbnail FROM articles me LEFT JOIN reads pindols ON ( me.id = pindols.article_id AND pindols.user_id = 9 ) WHERE ( pindols.user_id IS NULL ) GROUP BY me.id, me.url, me.title, me.content, me.last_update, me.author, me.thumbnail ORDER BY last_update DESC LIMIT ?) me LEFT JOIN reads pindols ON ( me.id = pindols.article_id AND pindols.user_id = 9 ) WHERE ( pindols.user_id IS NULL ) ORDER BY last_update DESC: '20'
Of course you can skip the paging but I had it in my code so I included it here.
Special thanks goes to deg from #dbix-class on irc.perl.org and https://blog.afoolishmanifesto.com/posts/dbix-class-parameterized-relationships/.
Thanks,
Canto

DBD::SQLite: Insert or Update - Question

While writing my question (howto: insert a new entry and if it exists already, update it) I found some answers in the Related Questions:
$sql = "INSERT OR REPLACE INTO $table ( Id, Name, Rating ) VALUES( ?, ?, ? )";
$sth_rating = $dbh->prepare( $sql );
$sth_rating->execute( $id, $name, $rating );
.
$sql = "INSERT OR IGNORE INTO $table ( Id, Name, Rating ) VALUES ( ?, ?, ? )";
$sth_rating = $dbh->prepare( $sql );
$sth_rating->execute( $id, $name, $rating );
$sql = "UPDATE $table SET Rating = ? WHERE Id = ?";
$sth_rating = $dbh->prepare( $sql );
$sth_rating->execute( $rating, $id );
Is the second method more safe then the first one?
The second method is less safe because it is non-atomic. In other words, it happens in more than one step. Consider two processes both updating the same data. Time is moving down.
Process 1 Process 2
INSERT OR IGNORE...
INSERT OR IGNORE...
UPDATE...
UPDATE...
Process 1 starts first, but Process 2 sneaks in and does its update between. Process 1 then blows right over Process 2's update.
This isn't so bad in this particular situation, both processes are going to blow over each other no matter what, but you can easily get yourself into trouble by extending your technique.
(Unless I misunderstood the question and what you want is an upsert)