I am trying to develop a custom plugin in word-press. I have created a form which calls the following script:
<?php
global $wpdb;
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
echo $sql;
//$wpdb->show_errors();
$wpdb->insert("wp_wbp_unpublished",array('uid'=>null,'pid'=>$_POST['bibkey'],
'author'=>$_POST['author'],'title'=>$_POST['title'],'year'=>$_POST['date'],
'note'=>$_POST['note'],'keyword'=> $_POST['keywords'],'abstract'=>"null"));
$wpdb->query($sql);
//$wpdb->print_error();
?>
As you can see, I have tried two different ways to insert data into my db but nothing worked.
The sql string is correct, I have checked it. But something wrong with the insert()/ query() commands. Any suggestions of what is wrong?
Honestly, I do not see anything wrong with your code. At the end of the day I always blame Wordpress for the fact that it sucks asshoe. I am having the same issue, except only after the user logs into the index file and tries to make changes to db. Why hasn't anyone answered this question? Wtf
You have to remove semicolon(); in your custom or first insert query
$sql="insert into wp_wbp_unpublished values(null,'".$_POST['bibkey']."',
'".$_POST['author']."','".$_POST['title']."','".$_POST['date']."','".$_POST['note']."',
'".$_POST['keywords']."','nothing');";
And other thing is when you make plugin, then you have to put "upgrade.php" file in you function...
I hope it will work.
I have used the following solution, which seems to be the best:
$wpdb->insert( $table, $data, $format );
For instance:
$wpdb->insert(
$prefix.'wp_wbp_unpublished',
array('bibkey'=>$_POST['bibkey'],'author'=>$author),
array('%s','%s')
);
More information: https://codex.wordpress.org/Class_Reference/wpdb#INSERT_row
Related
I have function that update post content automatically based from custom field like this:
function update_content(){
$mycustomfield = get_post_meta( get_the_ID(), 'customfield', true);
$post = array();
$post['ID'] = get_the_ID();
$post['post_content' ] = $mycustomfield ;
$post['post_title' ] = $mycustomfield ;
// Update the post into the database
wp_update_post( $post );
We update only custom field to make content. For now, we launch this function manually on save_post hook, but the articles are so many and we need now a cron to automate this function: process 4 posts every hour until all posts are completed, then start over.
How to make this, thank you
WordPress actually has a built-in psuedo cron system called WP Cron. It doesn't function exactly like a proper server cron, but can perform a similar function in many cases. You can find documentation on it here:
https://developer.wordpress.org/plugins/cron/#:~:text=WP%2DCron%20is%20how%20WordPress,post%2C%20utilize%20WP%2DCron.&text=WP%2DCron%20works%20by%20checking,what%20needs%20to%20be%20run.
However thinking about your use case and looking at your function above, I'm wondering what the purpose of your cron is? It looks from that function like all you're doing is taking some content already in your database and putting it somewhere else. Why? Why not simply display your custom field in the correct spot? Or better yet, use the fields as intended?
Even if that is necessary - maybe I don't understand fully from the example above - I think your initial inclination to run this on save_post is much more correct. Unless there's some information external to your site that's changing, the only time these values will change is when you save the post. So what is the purpose of running it on a schedule?
I'm in need of some help.
I want to display the category image on the current category page, and I have googled this, and each answer I find uses the same code.
They all use get_woocommerce_term_meta to retrieve the ID of the thumbnail used so that you can then use wp_get_attachment_url to get the image address.
All sounds great, but whenever I try this code it returns nothing, and I think that it is because get_woocommerce_term_meta is deprecated.
Does anyone know of a way round this, so I can get the image address when I have the category ID?
This is the code that I have in place:
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta($cat->term_id, 'thumbnail_id', true);
echo $cat->term_id."<br />";
echo $thumbnail_id;
$image_src = wp_get_attachment_url($thumbnail_id);
$cat->term_id returns the correct ID of the category, but $thumbnail_id returns 0.
The code is in header.php.
get_woocommerce_term_meta() may be deprecated however it hasn't yet been removed. The issue is elsewhere in your setup.
With that said we can resolve the deprecation issue quite easily. Simply replace usages of get_woocommerce_term_meta() with the new native WordPress function get_term_meta().
get_woocommerce_term_meta() will simply pass its arguments on to the new function anyway so we can be sure the problem isn't with the deprecated function.
Likely causes of the issue:
Key used to save the image isn't the same as the key being used to retrieve
Not passing in correct term ID
No image set
I have been trying to associate CUSTOM data to each of my WP POSTS using the following piece of code :
if($condition === true){
if ( ! update_post_meta ($post_id, '_someData', $someVariable) ) {
add_post_meta($post_id, '_someData', $someVariable);
}
}
However, seems like the META VALUE is RESET to default i.e. Zero OR Blank. Our WordPress website has around 40 plugins, and I think one of these WordPress plugins, is coming in my way of doing things. All of my logic works fine, on a demo WordPres website. Is there a way for me to have total control to set the META Value for a given POST ? Also, is there a way where I can be notified that the META Key is about to change and then I can decide whether OR not to change the Meta Value ?
Any pointers or reference URL's can be of great help. Thanks !
You only need to call update_post_meta in either scenario. Calling add_post_meta() is not necessary and could be causing this problem.
From the codex:
The function update_post_meta() updates the value of an existing meta key (custom field) for the specified post.
This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its result is returned.
I have seen almost all links but still I am unable to solve my problem.
I am getting $wpdb as null.
I am checking it like this. I am doing this in single.php file
echo "<pre>";print_r($wpdb);"</pre>";
I have checked about following files. That all are loaded.
wp-config.php
wp-load.php
wp-includes/wp-db.php
Please help me.
EDIT
I want to execute custom query like this.
$entries = $wpdb->get_results( $wpdb->prepare( "SELECT forms.form_title, entries. *
FROM wp_visual_form_builder_forms AS forms
INNER JOIN wp_visual_form_builder_entries AS entries ON entries.form_id = forms.form_id" ) );
this is not working.
If you read the documentation, you will notice this passage:
Always use the global $wpdb variable. (Remember to globalize $wpdb before using it in any custom functions.)
It's not very clear but I think that means you cannot use $wpdb outside of a function. What I recommend you do is create a function in your theme's functions.php file, and call that function from the single.php file.
$query = "CALL ..........";
$result = $wpdb->get_results($query);
print_r($results);
Is returning an empty array, how do you run stored procedures from the $wpdb class?
I'm not 100% sure since I've not worked with stored procs in MySQL yet (although I have coded many a stored-proc for MS-SQL in the 90's), but try:
global $wpdb;
$wpdb->query('CALL ...');
print_r($wpdb->last_result);
Whatever you pass to $wpdb->query() gets passed to the PHP mysql_query() function so whatever works there will work with WordPress' $wpdb->query(). I do know this works:
global $wpdb;
$wpdb->query('SELECT * FROM wp_posts LIMIT 10');
print_r($wpdb->last_result);
If you want to read the source code for $wpdb->query() to further understand what is happening you can find it in /wp-includes/wp-db.php at lines 1057-1153 in v3.01.
Hope this helps.
-Mike
P.S. You might want to check out StackOverflow's sister site WordPress Answers next time you need help with WordPress. Lots of WordPress enthusiasts are on hand over there to answer your WordPress questions.