May 4, 2011

Possible to check post custom field value before executing php in tools global post mod? | CyberSEO Pro | Support Forum

Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_TopicIcon
Possible to check post custom field value before executing php in tools global post mod?
Topic Rating: 0 Topic Rating: 0 Topic Rating: 0 Topic Rating: 0 Topic Rating: 0 Topic Rating: 0 (0 votes) 
September 8, 2014
2:06 am
Avatar
vrocks
Guest
Guests

Possible to check post custom field value before executing php in tools global post mod?

Is it possible to check for a custom field to be for example: switched = 1

Then if found, run the PHP code?

September 8, 2014
2:28 am
Avatar
CyberSEO
Admin
Forum Posts: 3913
Member Since:
July 2, 2009
sp_UserOfflineSmall Offline

Sure. Like this:

Login to see the code

September 8, 2014
2:42 am
Avatar
vrocks
Guest
Guests

That gives me:

Modifying existing posts. Please wait... Fatal error: Cannot use object of type stdClass as array in /home/domains/xxx/httpdocs/wp-content/plugins/CyberSEO/cyberseo-tools.php(0) : runtime-created function on line 1

I have 3 posts with no custom fields. And one post with that custom field.

I am trying to get the plugin to only affect posts with that custom field.

 

I also tried accessing it as an object, but it doesn't return true for that one post and run.

September 8, 2014
3:05 am
Avatar
vrocks
Guest
Guests

if(!isset($post->custom_fields->switched )) {

$post->custom_fields->switched = 2;
}

The switched = 2 gives an error... 

So I used it to check the comparison. With the code above it should spit out 3 errors because one post has its custom_fields: switched set to 1. So that one should return as set and the other not set and the code should only be run 3 times.

It is being run 4 times which means $post->custom_fields->switched is not being set by CyberSEO. It isn't grabbing that from the DB to make this check it seems.

September 8, 2014
3:20 am
Avatar
CyberSEO
Admin
Forum Posts: 3913
Member Since:
July 2, 2009
sp_UserOfflineSmall Offline

You didn't mention you want to apply this code to the existing posts. What exactly error messages did you get?

September 8, 2014
3:24 am
Avatar
vrocks
Guest
Guests

Well, I specified in the title and the first like that this was for the Tools, Global post mod.

 

But anyway...

Modifying existing posts. Please wait... Warning: addslashes() expects parameter 1 to be string, object given in /home/domains/xxx/httpdocs/wp-includes/formatting.php on line 3392 Warning: addslashes() expects parameter 1 to be string, object given in /home/domains/xxx/httpdocs/wp-includes/formatting.php on line 3392 Warning: addslashes() expects parameter 1 to be string, object given in /home/domains/xxx/httpdocs/wp-includes/formatting.php on line 3392 Warning: addslashes() expects parameter 1 to be string, object given in /home/domains/xxx/httpdocs/wp-includes/formatting.php on line 3392 Done!

 

And if I modify the code:

$post['custom_fields']['switched'] = 2;

 

I get error:

Modifying existing posts. Please wait... Fatal error: Cannot use object of type stdClass as array in /home/domains/xxx/httpdocs/wp-content/plugins/CyberSEO/cyberseo-tools.php(0) : runtime-created function on line 3

September 8, 2014
3:31 am
Avatar
CyberSEO
Admin
Forum Posts: 3913
Member Since:
July 2, 2009
sp_UserOfflineSmall Offline

Because you should use the object:

Login to see the code

September 8, 2014
4:14 am
Avatar
vrocks
Guest
Guests

Doing so gives  the original error at the top of my last reply.

September 8, 2014
4:30 am
Avatar
CyberSEO
Admin
Forum Posts: 3913
Member Since:
July 2, 2009
sp_UserOfflineSmall Offline

Oops. Sorry, in the "Global Posts Modification Tools" you have to do it via WordPress API's only. E.g.:

Login to see the code

September 8, 2014
5:01 am
Avatar
vrocks
Guest
Guests
10sp_Permalink sp_Print
0

That worked. Thanks!

Might want to update pages like this one: Login to see this link

To also talk about the wordpress API being needed to get the ID on the global changer. Had it been there or that other page (6?) of some documentation that came up in Google I would have figured it out without needing to bother you.

Forum Timezone: Europe/Amsterdam

Most Users Ever Online: 541

Currently Online:
13 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

ninja321: 84

s.baryshev.aoasp: 68

Freedom: 61

Pandermos: 54

MediFormatica: 49

B8europe: 48

Member Stats:

Guest Posters: 337

Members: 2818

Moderators: 0

Admins: 1

Forum Stats:

Groups: 1

Forums: 5

Topics: 1627

Posts: 8280

Newest Members:

bkc.tessier, igor.buzaev, jeremyboucher, lthompson2709, MAC, duty3canada

Administrators: CyberSEO: 3913