admin管理员组

文章数量:1326123

So I am working on a something like a plugin in my custom wordpress theme, and and in an options page I have a form whose action is the php file _options.php, in normal manner some data will be sent to this php file to edit meta data of some posts but this didn't work, So I really did a very basic job in the _options.php

I just added this line of code to the _options.php file

update_post_meta( 44, 'order_meta', 2);

So in theory whenever I open this php file in browser it should go to the post with ID 44 and edit the 'order_meta' metadata value to 2, or at least this is my understanding, but it doesn't work.

Also if I added another line of code after this line it doesn't execute, for example when I run this

update_post_meta( 44, 'order_meta', 2);
echo 'Testing text';

The "Testing text" is not echoed or anything, so I think this means that the above function is exiting the execution or something like that. I don't ave much background experience with php so you can consider me a beginner but I don't know why it doesn't work.

The Post with the id 44 is already present, and the meta with key order_meta already has a value of 1 but it doesn't seem want to change.

In short my question is how to edit a post meta data programmatically? Well my only guess is that I should somehow activate post editing when using this function which I don't know how to do, please if you know the answer please tell me.

Thanks is advance.

So I am working on a something like a plugin in my custom wordpress theme, and and in an options page I have a form whose action is the php file _options.php, in normal manner some data will be sent to this php file to edit meta data of some posts but this didn't work, So I really did a very basic job in the _options.php

I just added this line of code to the _options.php file

update_post_meta( 44, 'order_meta', 2);

So in theory whenever I open this php file in browser it should go to the post with ID 44 and edit the 'order_meta' metadata value to 2, or at least this is my understanding, but it doesn't work.

Also if I added another line of code after this line it doesn't execute, for example when I run this

update_post_meta( 44, 'order_meta', 2);
echo 'Testing text';

The "Testing text" is not echoed or anything, so I think this means that the above function is exiting the execution or something like that. I don't ave much background experience with php so you can consider me a beginner but I don't know why it doesn't work.

The Post with the id 44 is already present, and the meta with key order_meta already has a value of 1 but it doesn't seem want to change.

In short my question is how to edit a post meta data programmatically? Well my only guess is that I should somehow activate post editing when using this function which I don't know how to do, please if you know the answer please tell me.

Thanks is advance.

Share Improve this question asked Aug 12, 2020 at 19:04 Shams M.MonemShams M.Monem 1113 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Directly accessing PHP files is not really how WordPress works.

You currently have your code in a random PHP file that has no connection to WordPress. It's probably throwing a Fatal error (Call to undefined function), which is why the next line doesn't get executed.

You said this code is in your theme, so I would start by putting that code in functions.php. If you want it in a different file, you'll need to add this to that functions.php file:

require_once '_options.php';

Or if it's in a folder in your theme (like "includes"), you'd do:

require_once 'includes/_options.php';

To control when that code is fired, you would use one of the many WordPress hooks.

For the correct way you create an options page, check out this article in the codex.

本文标签: post metaHow to use updatepostmeta() function properly