Let’s see if this works. I can’t really test this, but there are some things to update in your code:
function set_activation_value(){
// Sets Field Defaults
$option = get_option('ews_index_option_name');
// get_option returns FALSE if it doesn't exist, so we check that here.
if ( $option === FALSE ) {
$args = array (
'public' => true
);
$post_types = get_post_types( $args );
// Since the option doesn't exist, we should add it, then update it in the loop.
add_option('ews_index_option_name');
// now we have an empty array.
$my_options = [];
$post_type_output="";
foreach ( $post_types as $post_type ) {
// no need to wrap in parenthesis for each condition.
if ( $post_type === 'page' || $post_type === 'product' || $post_type === 'post' ) {
$my_options[ $post_type ] = 'index';
update_option('ews_index_option_name', $my_options);
// Skip the else if, since every other post type is no index.
} else {
$my_options[ $post_type ] = 'no-index';
update_option('ews_index_option_name', $my_options);
}
$post_type_output .= $post_type;
}
// These look to be holding the exact same data on one in an array and the other a string.
update_option('ews_index_my_types', $post_types);
update_option('ews_index_option_var', $post_type_output);
}
}
register_activation_hook( __FILE__, 'set_activation_value' );
I cleaned it up a bit and added comments. Also, there is no reason to have wp_reset_postdata()
.
As said, I can’t test this, but this might work better for you.