Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Automatically Editing 2 Customfields After Publish Date Via Cron WordPress

  • SOLVED

I would like to run a cron in Wordpress to edit/update automatically 2 custom-fields of all posts in relation to the age of the posts (after 60 days of age). It can be one hook, that combines the two customfield updates or two seperated hooks in cron!

<strong>These are the 2 customfield variables:</strong>

<strong>Customfield 1</strong>
<em>Before updating:</em>

_meta_key_1 | before_meta_value_1

<em>After updating:</em>

_meta_key_1 | after_meta_value_1

<strong>Customfield 2</strong>

<em>Before updating:</em>

_meta_key_2 | before_meta_value_2

<em>After updating:</em>

_meta_key_2 | after_meta_value_2

<strong>I found this to delete posts with a specific age:</strong>

add_action( 'my_trash_hook', 'my_trash_hook_function' );

if (!wp_next_scheduled('my_trash_hook')) {
wp_schedule_event( time(), 'weekly', 'my_trash_hook' );
}

function my_trash_hook_function() {

global $wpdb,$post;

$daystogo = "365"; // trash post older than daysago = 12 months
$user_id="1"; // trash post from user with user_id

$sql = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'trash' WHERE post_status='publish' AND ID != '206' AND ID != '63527' AND post_type = 'post' AND post_author='%s' AND DATEDIFF(NOW(), post_date) > %d",$user_id,$daystogo);


$wpdb->query($sql);}


<strong>So i tried this for one custom field, but it doesn´t work! Nothing happened, after running the cron in WP:</strong>



add_action( 'my_noindex_hook', 'my_noindex_function' );

if (!wp_next_scheduled('my_noindex_hook')) {
wp_schedule_event( time(), 'weekly', 'my_noindex_hook' );

function my_noindex_function() {


global $wpdb;

$daystogo = "60";

$sql = $wpdb->prepare("UPDATE $wpdb->postmeta
SET $wpdb->postmeta.meta_value = 'after_meta_value_1'
WHERE $wpdb->postmeta.post_id in(
SELECT $wpdb->postmeta.meta_value
FROM $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_meta_key_1'
AND $wpdb->postmeta.meta_value='before_meta_value_1'
AND $wpdb->posts.ID != '206'
AND $wpdb->posts.ID != '63527'
AND DATEDIFF(NOW(), post_date) > %d)",$daystogo);


$wpdb->query($sql);
}

It would be nice, if someone can help me.


Answers (1)

2012-12-19

Dbranes answers:

Hi, does your query work as you want?

i.e. have you tried to run it directly, fx in phpmyadmin?

$sql = $wpdb->prepare("UPDATE $wpdb->postmeta
SET $wpdb->postmeta.meta_value = 'after_meta_value_1'
WHERE $wpdb->postmeta.post_id in(
SELECT $wpdb->postmeta.meta_value
FROM $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_meta_key_1'
AND $wpdb->postmeta.meta_value='before_meta_value_1'
AND $wpdb->posts.ID != '206'
AND $wpdb->posts.ID != '63527'
AND DATEDIFF(NOW(), post_date) > %d)",$daystogo);

$wpdb->query($sql);


<strong>update:</strong>

To debug you it further you can fx:

- let it send a mail to you when it's activated in the wp-cron (see fx http://codex.wordpress.org/Function_Reference/wp_mail)
- use hourly instead of weekly
- test the query in phpmyadmin
- use the cron plugins (fx http://wordpress.org/extend/plugins/wp-crontrol/screenshots/ )


emti comments:

No i haven´t tested it. What means fx? Can i insert and run it like you wrote in phpmyadmin?


Dbranes comments:

you have to <strong>echo $sql;</strong> first, it might give you fx:

UPDATE wp_postmeta SET wp_postmeta.meta_value = 'after_meta_value_1' WHERE wp_postmeta.post_id in( SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_meta_key_1' AND wp_postmeta.meta_value='before_meta_value_1' AND wp_posts.ID != '206' AND wp_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) > 60)


and then you can run it in the "sql" box in fx phpmyadmin.

You can also check this part first:

SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_meta_key_1' AND wp_postmeta.meta_value='before_meta_value_1' AND wp_posts.ID != '206' AND wp_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) > 60


emti comments:

I´m allready using wp-crontrol and klicked the "run now" button several times on the overview site. The code for deleting posts after a specific post age worked well!

I think i have an error in my self made code for the new function, beacause i´m not in to php! Does it look right to you?


Dbranes comments:

you might have an error here:

if (!wp_next_scheduled('my_noindex_hook')) {
wp_schedule_event( time(), 'weekly', 'my_noindex_hook' );


replace it with fx:

if (!wp_next_scheduled('my_noindex_hook')) {
wp_schedule_event( time(), 'weekly', 'my_noindex_hook' );
}


i.e. missing "}"


Dbranes comments:

try replacing this:

SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_meta_key_1' AND wp_postmeta.meta_value='before_meta_value_1' AND wp_posts.ID != '206' AND wp_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) > 60


with:

SELECT wp_postmeta.meta_value FROM wp_postmeta,wp_posts WHERE wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = '_meta_key_1' AND wp_postmeta.meta_value='before_meta_value_1' AND wp_posts.ID != '206' AND wp_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) > 60

or

SELECT wp_postmeta.meta_value FROM wp_postmeta LEFT JOIN wp_posts on wp_posts.ID = wp_postmeta.post_id WHERE wp_postmeta.meta_key = '_meta_key_1' AND wp_postmeta.meta_value='before_meta_value_1' AND wp_posts.ID != '206' AND wp_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) > 60


emti comments:

Ok, I´ve tested it in phpmyadmin:

code 1

UPDATE kl_postmeta SET kl_postmeta.meta_value = '2' WHERE kl_postmeta.post_id in( SELECT kl_postmeta.meta_value FROM kl_postmeta WHERE kl_posts.ID = kl_postmeta.post_id AND kl_postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND kl_postmeta.meta_value='1' AND kl_posts.ID != '206' AND kl_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60)

-> ERROR: #1054 - Unknown column 'kl_posts.ID' in 'where clause'

Your last two SELECT actions gives me an list back with "meta_value" column! I´ve switched "> 60" to "< 60", because actually i haven´t posts, which are older then 60 days. And you´ll have noticed, that my tables starting with "kl_".

Thank you so far, What should i do next?




Dbranes comments:

does this run:

UPDATE kl_postmeta SET kl_postmeta.meta_value = '2' WHERE kl_postmeta.post_id in( SELECT kl_postmeta.meta_value FROM kl_postmeta, kl_posts WHERE kl_posts.ID = kl_postmeta.post_id AND kl_postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND kl_postmeta.meta_value='1' AND kl_posts.ID != '206' AND kl_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60)


Dbranes comments:

meant this ;-)

UPDATE kl_postmeta SET kl_postmeta.meta_value = '2' WHERE kl_postmeta.post_id in( SELECT kl_postmeta.post_id FROM kl_postmeta, kl_posts WHERE kl_posts.ID = kl_postmeta.post_id AND kl_postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND kl_postmeta.meta_value='1' AND kl_posts.ID != '206' AND kl_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60)


Your select query must return a list of post_id's, since your update query is picking up all post_id's from that list.


emti comments:

ERROR: #1093 - You can't specify target table 'kl_postmeta' for update in FROM clause


emti comments:

Same error for both actions!


Dbranes comments:

ps: should of course have backup of your db, before trying all this out ;-)

ok, what about this:

UPDATE kl_postmeta SET kl_postmeta.meta_value = 'after_meta_value_1'
WHERE kl_postmeta.post_id in
(
SELECT post_id from (
SELECT kl_postmeta.post_id FROM kl_posts LEFT JOIN kl_postmeta ON kl_posts.ID = kl_postmeta.post_id WHERE kl_postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND kl_postmeta.meta_value='1' AND kl_posts.ID != '206' AND kl_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60 group by post_id
) t
)


emti comments:

No that didn´t work!

I had to restore my DB, because it seemed to be destroyed! The meta_keys/meta_values seemed to be crashed.


Dbranes comments:


step 1: is this giving you the right post_id's ?

SELECT post_id from (
SELECT kl_postmeta.post_id FROM kl_posts LEFT JOIN kl_postmeta ON kl_posts.ID = kl_postmeta.post_id WHERE kl_postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND kl_postmeta.meta_value='1' AND kl_posts.ID != '206' AND kl_posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60 group by post_id

) t


step 2: I think we forgot to restrict to the right meta_key, like this:

UPDATE kl_postmeta SET kl_postmeta.meta_value = 'after_meta_value_1'
WHERE kl_postmeta.meta_key = '_meta_key_1' AND kl_postmeta.post_id in
( ... same as above ... )




emti comments:

Yes, step 1 gives a list of the right postids!

Step2: This time the query seems to work. All posts are now set to the right value!

So should this work or do i have to edit anywhere in the code?

// run the function "my_noindex_hook_function" with wp-cron

add_action( 'my_noindex_hook', 'my_noindex_function' );

if (!wp_next_scheduled('my_noindex_hook')) {
wp_schedule_event( time(), 'weekly', 'my_noindex_hook' );
}

// the function we want to run

function my_noindex_function() {

global $wpdb;

$sql = $wpdb->prepare("UPDATE $wpdb->postmeta SET $wpdb->postmeta.meta_value = '2'
WHERE $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND $wpdb->postmeta.post_id in
(
SELECT post_id from (

SELECT $wpdb->postmeta.post_id FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id WHERE $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex' AND $wpdb->postmeta.meta_value='1' AND $wpdb->posts.ID != '206' AND $wpdb->posts.ID != '63527' AND DATEDIFF(NOW(), post_date) < 60 group by post_id
) t
)");
$wpdb->query($sql);
}


Dbranes comments:

ok great, we are heading in the right direction ;-)

The next step could be to test the function on your site.

You could fx. place this into your functions.php file:

// test my function
my_noindex_function();


and refresh the page once, and then remove the function call.

If this is updating correctly, then the next step is to activate it in the wp-cron (I would first try 'hourly' instead of 'weekly').


emti comments:

OK, I´ve put the code in functions.php as you said:

// test my function

my_noindex_function();


A server error is resulting, but what is it for? Isn´t it normal, that an error results, if you don´t have defined this new function?


Dbranes comments:

You should have all your code (i.e. the latest code block you posted) in functions.php, and make sure you only have
the "my_noindex_function" defined only once. Make sure the php code is correct.

The last code block is running ok, on my wp install.

You could also check your error_log for further info, paste it here if you know how to find it.


emti comments:

No i don´t know where to find the error_log.. Is it "on" by default?

I´ve tested it, too now and it seems to run!

But could it be, that it takes a little bit longer to run this code over cron, than directly via phpmyadmin?
I´ve many posts so it´s noticeable! It takes felt like 3 times more to complete this action.


Dbranes comments:

ok, so the "server error" is not any more and the site is ok?

I have not detected a major difference with wp-cron vs phpmyadmin?

(I usually use the cron feature directly on the Linux instead of wp-cron)

You can fx put these line into your wp-config.php

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);


so that all errors should be saved to a debug.log log file inside the /wp-content/ directory.

see more here:

http://codex.wordpress.org/Debugging_in_WordPress

ps: the apache error log is also usually accessible from the CPanel.


emti comments:

I´ve noticed, that new posts doesn´t have my needed custom metas!

<strong>So i need a modification: </strong>
if meta_key and meta_value does not exist, then it must be inserted otherwise just update as before.

One other thing is, that i have excluded two posts, which are in my "blog" category.

AND $wpdb->posts.ID != '206' AND $wpdb->posts.ID != '63527'

The better way would be to exclude all posts, which are in the "blog" category with the id = "25".


Dbranes comments:

ok, you can try this sql command to ignore posts in the category "blog" :

$sql="
UPDATE {$wpdb->postmeta} SET {$wpdb->postmeta}.meta_value = '2'
WHERE {$wpdb->postmeta}.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND {$wpdb->postmeta}.post_id in
(SELECT post_id from (
SELECT
{$wpdb->term_relationships}.object_id as post_id,
{$wpdb->posts}.post_title,
{$wpdb->terms}.name as term_name,
{$wpdb->term_taxonomy}.term_id as term_id,
{$wpdb->term_taxonomy}.taxonomy,
{$wpdb->terms}.slug as term_slug
FROM {$wpdb->terms}
LEFT JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
LEFT JOIN {$wpdb->term_relationships}
ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->posts}
ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.id
LEFT JOIN {$wpdb->postmeta}
ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
WHERE ({$wpdb->term_taxonomy}.taxonomy != 'category' AND {$wpdb->terms}.slug != 'blog' )
AND $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND $wpdb->postmeta.meta_value='1'
AND {$wpdb->posts}.post_status ='publish'
AND DATEDIFF(NOW(), post_date) < 60
GROUP BY post_id
) as t
)
";


or if you have id=25 (for the category "blog"), it might be faster:

$sql="
UPDATE {$wpdb->postmeta} SET {$wpdb->postmeta}.meta_value = '2'
WHERE {$wpdb->postmeta}.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND {$wpdb->postmeta}.post_id in
(SELECT post_id from (
SELECT
{$wpdb->term_relationships}.object_id as post_id,
{$wpdb->posts}.post_title,
{$wpdb->term_taxonomy}.term_id as term_id,
{$wpdb->term_taxonomy}.taxonomy
FROM {$wpdb->term_relationships}
LEFT JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->posts}
ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.id
LEFT JOIN {$wpdb->postmeta}
ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
WHERE ({$wpdb->term_taxonomy}.taxonomy != 'category' AND {$wpdb->term_taxonomy}.term_id != 25 )
AND $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND $wpdb->postmeta.meta_value='1'
AND {$wpdb->posts}.post_status ='publish'
AND DATEDIFF(NOW(), post_date) < 60
GROUP BY post_id
) as t
)
";


Dbranes comments:

you could try this kind of action hook to automatically insert post meta key/value
(i.e. '_yoast_wpseo_meta-robots-noindex'/2) for new posts:

add_action( 'save_post', 'my_add_postmeta_onsave' );

function my_add_postmeta_onsave( $post_id ) {
global $post;
// only for post_type 'post'
if ($post->post_type != 'post')
return;
// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// AJAX? Not used here
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;

// get post_meta
$key_value = get_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', true);

// check if the custum field hasn't got a value
if(strlen($key_value) == 0) {
add_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', '2');
}
}


emti comments:

Ok, I´ve tested your solutions.

1. Is there a light weighter variant of this one? I´ve many postss and this version takes much more time to load (6x longer) than the code before with excluding postids. If not, i will have to use the one before.

I mean this one:

$sql="

UPDATE {$wpdb->postmeta} SET {$wpdb->postmeta}.meta_value = '2'

WHERE {$wpdb->postmeta}.meta_key = '_yoast_wpseo_meta-robots-noindex'

AND {$wpdb->postmeta}.post_id in

(SELECT post_id from (

SELECT

{$wpdb->term_relationships}.object_id as post_id,

{$wpdb->posts}.post_title,

{$wpdb->term_taxonomy}.term_id as term_id,

{$wpdb->term_taxonomy}.taxonomy

FROM {$wpdb->term_relationships}

LEFT JOIN {$wpdb->term_taxonomy}

ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id

LEFT JOIN {$wpdb->posts}

ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.id

LEFT JOIN {$wpdb->postmeta}

ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id

WHERE ({$wpdb->term_taxonomy}.taxonomy != 'category' AND {$wpdb->term_taxonomy}.term_id != 25 )

AND $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex'

AND $wpdb->postmeta.meta_value='1'

AND {$wpdb->posts}.post_status ='publish'

AND DATEDIFF(NOW(), post_date) < 60

GROUP BY post_id

) as t

)

";


2. Your idea of setting the none existing metakeys/metavalues automatically directly with publishing the post is great, but unfortunately it doesn´t work for me. I´ve now noticed, that new posts, which are created automatically via my Feed-Plugin doesn´t include these metas for whatever reason.

After updating the post manually the metas by default are shown in my database. It seems, that they were allready there, but invisible. I come to this conclusion, that they were a kind of invisible, because after updating new crated posts manually the new values, which are set via your function in functions.php aren´t set! So they have to been there before, right?! Little confusing!

I think the only solution were to make a cron as well! What do you think? Would be nice.


Dbranes comments:

Try replacing

AND DATEDIFF(NOW(), post_date) < 60


with

AND post_date < '2012-10-20'


to test if this is any faster. (then the date could be generated in php before running the sql)

One could also try to have limit 0,500 for each run.

Try also the 4 table join query instead of the 5 table join query above.

fx:

$sql="
UPDATE {$wpdb->postmeta} SET {$wpdb->postmeta}.meta_value = '2'
WHERE {$wpdb->postmeta}.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND {$wpdb->postmeta}.post_id in
(SELECT post_id from (
SELECT
{$wpdb->term_relationships}.object_id as post_id
FROM {$wpdb->term_relationships}
LEFT JOIN {$wpdb->term_taxonomy}
ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id
LEFT JOIN {$wpdb->posts}
ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.id
LEFT JOIN {$wpdb->postmeta}
ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
WHERE {$wpdb->term_taxonomy}.term_id != 25
AND $wpdb->postmeta.meta_key = '_yoast_wpseo_meta-robots-noindex'
AND $wpdb->postmeta.meta_value='1'
AND {$wpdb->posts}.post_status ='publish'
AND post_date < '2012-10-20'
GROUP BY post_id
) as t
)
";

ps: I sometime use "fx" as an abbreviation for "for example" ;-)


Dbranes comments:

<blockquote>2. Your idea of setting the none existing metakeys/metavalues automatically directly with publishing the post is great, but unfortunately it doesn´t work for me. I´ve now noticed, that new posts, which are created automatically via my Feed-Plugin doesn´t include these metas for whatever reason.

After updating the post manually the metas by default are shown in my database. It seems, that they were allready there, but invisible. I come to this conclusion, that they were a kind of invisible, because after updating new crated posts manually the new values, which are set via your function in functions.php aren´t set! So they have to been there before, right?! Little confusing!
</blockquote>

You can add this code to your functions.php to check out the meta data.

It will create a metabox and print out the current meta data.

add_action( 'add_meta_boxes', 'my_postmeta_metabox' );

function my_postmeta_metabox(){
add_meta_box( 'my_postmeta_metabox', 'Current post meta data' , 'my_postmeta_metabox_callback', 'post', 'normal', 'high' );
}

function my_postmeta_metabox_callback(){
global $post;
// let's print out all the meta data, to check if we see '_yoast_wpseo_meta-robots-noindex'
echo "<pre>";
print_r(get_post_meta($post->ID));
echo "</pre>";
}


emti comments:

Ok, The 4-table variant in combination with

AND DATEDIFF(NOW(), post_date) < 60

works and this very quick - like 10 sec instead of 10 minutes :) So that´s fine now.

<strong>metakeys on autogenerated posts:</strong>
No, the needed metas aren´t shown on this auto generated posts! There is only this metakey "_yoast_wpseo_linkdex", what could most likely be relevant for us. I´ve googled it: It´s a full content SEO analysis functionality, which i don´t have in use.


Dbranes comments:

You could try this action

add_action( 'wp_insert_post', 'my_add_postmeta_onsave' );

to see if the '_yoast_wpseo_meta-robots-noindex' meta key is generated for automatically inserted posts.


emti comments:

add_action( 'wp_insert_post', 'my_add_postmeta_onsave' );

instead of

add_action( 'save_post', 'my_add_postmeta_onsave' );

didn´t work. The posts still haven´t the customfields!


Dbranes comments:

hmm..., what's the url to the Feed plugin, so I can check out how the posts are inserted,
maybe it's not via the wp_insert_posts function?


emti comments:

I´ve send you a message...


emti comments:

Maybe it´s the best, to make a seperate cron to insert the metas, if doesn´t exist on posts and is not in category "25"?!


Dbranes comments:


ok, you should try this hook, this is working for me ;-)

(you can uncomment the debug-logging inside it, when you have updated the path to the logger.txt file and make it writeable)

add_action( 'wp_insert_post', 'my_add_postmeta_oninsert' );
function my_add_postmeta_oninsert( $post_id ) {

// only for post_type 'post'
if (get_post_type( $post_id ) != 'post')
return;
// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// AJAX? Not used here
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
// Check user permissions
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;

// get post_meta
$key_value = get_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', true);

// check if the custum field has a value
if(strlen($key_value) == 0) {
add_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', '2');
}

// debug:
//my_logger("wp_insert_post check");
}

function my_logger($var){
file_put_contents('/some/path/logger.txt', date("Y-m-d: H:I:s",time())." --- ".print_r($var, true), FILE_APPEND);
}



emti comments:

I´ve added this to my functions.php like this:

// debug:

//my_logger("wp_insert_post check");
}
function my_logger($var){

file_put_contents('/is/htdocs/XXXX_XXXX/wordpress/XXXX/wp-content/themes/XXXXX/logger.txt', date("Y-m-d: H:I:s",time())." --- ".print_r($var, true), FILE_APPEND);

}


No change: autogenerated posts doesn´t have an metakey "_yoast_wpseo_meta-robots-noindex'"!
In addition i´ve created the logger.txt file writable (777), but it stays empty!

Did you test it with running yoast SEO plugin without updating manually?


Dbranes comments:

The above code gave me logging lines, but I forgot to check the meta ;-)

This gives me logging lines and meta key:

add_action( 'wp_insert_post', 'my_add_postmeta_oninsert' );
function my_add_postmeta_oninsert( $post_id ) {

// only for post_type 'post'
if (get_post_type( $post_id ) != 'post')
return;

// Autosave, do nothing
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;

// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;

// get post_meta
$key_value = get_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', true);

// check if the custum field has a value
if(strlen($key_value) == 0) {
add_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', '2');
}

// debug:
//my_logger("wp_insert_post for post_id=".$post_id."\n");
}


This gives me logging lines like these

2012-12-21: 00:0:47 --- wp_insert_post for post_id=759
2012-12-21: 00:0:10 --- wp_insert_post for post_id=769
2012-12-21: 00:0:10 --- wp_insert_post for post_id=770


when I import items from an external feed:

I have Multi Feed plugin installed but not yoast SEO.

ps: you can always try this (and restrict later)

add_action( 'wp_insert_post', 'my_add_postmeta_oninsert' );
function my_add_postmeta_oninsert( $post_id ) {

// get post_meta
$key_value = get_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', true);

// check if the custum field has a value
if(strlen($key_value) == 0) {
add_post_meta($post_id, '_yoast_wpseo_meta-robots-noindex', '2');
}

// debug:
//my_logger("wp_insert_post for post_id=".$post_id."\n");
}



emti comments:

Looks good to me :) Thank you


emti comments:

I´ve tried to exclude the category "25" from the my_trash_hook function at the beginning:

add_action( 'my_trash_hook', 'my_trash_hook_function' );

if (!wp_next_scheduled('my_trash_hook')) {

wp_schedule_event( time(), 'weekly', 'my_trash_hook' );

}

function my_trash_hook_function() {

global $wpdb,$post;

$daystogo = "365"; // trash post older than daysago = 12 months

$user_id="1"; // trash post from user with user_id

$sql = $wpdb->prepare("UPDATE $wpdb->posts SET post_status = 'trash' WHERE post_status='publish' AND ID != '206' AND ID != '63527' AND post_type = 'post' AND post_author='%s' AND DATEDIFF(NOW(), post_date) > %d",$user_id,$daystogo);

$wpdb->query($sql);}


To make all functions the same with excluding posts by this category_id = "25". How to edit this lines? I´ve tried it by myself, but wasn´t successful.


emti comments:

Tested this

// START my_trash_hook_function

add_action( 'my_trash_hook', 'my_trash_hook_function' );

if (!wp_next_scheduled('my_trash_hook')) {
wp_schedule_event( time(), 'weekly', 'my_trash_hook' );
}

function my_trash_hook_function() {

global $wpdb,$post;

$daystogo = "365";

// trash post from user with user_id

$user_id="1";

$sql="UPDATE {$wpdb->posts} SET {$wpdb->posts}.post_status = 'trash' WHERE {$wpdb->posts}.id in (SELECT post_id from ( SELECT {$wpdb->term_relationships}.object_id as post_id FROM {$wpdb->term_relationships} LEFT JOIN {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id LEFT JOIN {$wpdb->posts} ON {$wpdb->term_relationships}.object_id = {$wpdb->posts}.id WHERE ({$wpdb->term_taxonomy}.taxonomy != 'category' AND {$wpdb->term_taxonomy}.term_id != 25 ) AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_author = %d AND DATEDIFF(NOW(), post_date) > %d GROUP BY post_id ) as t ) ";

$wpdb->query($wpdb->prepare($sql,$user_id,$daystogo));

}


But it seems, that nothing happend, even if i set - No change to posts:

$daystogo = "1";


emti comments:

Ok, tested:

SELECT kl_term_relationships.object_id as post_id,kl_posts.post_title FROM kl_term_relationships LEFT JOIN kl_term_taxonomy ON kl_term_taxonomy.term_taxonomy_id = kl_term_relationships.term_taxonomy_id LEFT JOIN kl_posts ON kl_term_relationships.object_id = kl_posts.id WHERE (kl_term_taxonomy.taxonomy != 'category' AND kl_term_taxonomy.term_id != 25 ) AND kl_posts.post_status = 'publish' AND kl_posts.post_author = 1 AND DATEDIFF(NOW(), post_date) > 1 GROUP BY post_id

It gives me all the posts back with coloumns: post_id and post_title