最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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:

What I expect to get is:

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!

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:

What I expect to get is:

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!

Share Improve this question asked Mar 25, 2020 at 17:33 Robert LRobert L 93 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

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.

发布评论

评论列表(0)

  1. 暂无评论