mercredi 25 mars 2020

Foreach Loop Of Post Types With Nested If Else If Not Completing Process

When my plugin is updated I am trying to write new values to the database. I am running through the post types and if it is Page Product or Post I want to place INDEX in to the DB. Else I want to place NO-INDEX into the DB. The INDEX part loops properly, or at least the best I can tell as the values are stored in the DB. But when it gets to the NO-INDEX, it processes the first post type, which is ATTACHMENTS, and then does not get or process the remaining post types. I have tried several variations on the code and cannot seem to get it to work correctly. What am I doing wrong here?

I am using add_option and register_setting

Here is the code:

function set_activation_value(){
    // Sets Field Defaults
    $option = get_option('ews_index_option_name');
    if (empty($option)) {
        $args = array (
            'public' => true
        ); 
        $post_types = get_post_types( $args, 'names' ); 
        $my_options = get_option('ews_index_option_name');
        $post_type_output = '';
        foreach ( $post_types as $post_type ) {
            if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
                $my_options = get_option('ews_index_option_name');
                $my_options["$post_type"] = 'index';
                update_option('ews_index_option_name', $my_options);
            }
            else if (($post_type != "page") && ($post_type != "product") && ($post_type != "post")) {
                $my_options = get_option('ews_index_option_name');
                $my_options["$post_type"] = 'no-index';
                update_option('ews_index_option_name', $my_options);
            }
            wp_reset_postdata();
            $post_type_output .= $post_type;
        }
        update_option('ews_index_my_types', $post_types);
        update_option('ews_index_option_var', $post_type_output);
    }
}
register_activation_hook( __FILE__, 'set_activation_value' );

When I run the update I get the following results:

posts='index' pages='index' attachments='no-index' wpm-testimonials='' rl-galleries=''

What I expect to get is:

posts='index' pages='index' attachments='no-index' wpm-testimonials='no-index' rl-galleries='no-index'

The database also shows the same data as what these pictures represent. I have also added other post types to check and on the else or if else it never gets past the attachments.

I have also tried the following code with no success:

foreach ( $post_types as $post_type ) {
    if (($post_type == "page") || ($post_type == "product") || ($post_type == "post")) {
        $my_options = get_option('ews_index_option_name');
        $my_options["$post_type"] = 'index';
        update_option('ews_index_option_name', $my_options);
    } else {
        $my_options = get_option('ews_index_option_name');
        $my_options["$post_type"] = 'no-index';
        update_option('ews_index_option_name', $my_options);
    }
}

Any help would be greatly appreciated, been at this for a while. I am not terribly good with loops and arrays, which may be apparent here. Thank you!

Aucun commentaire:

Enregistrer un commentaire