检查发布/更新帖子上的复选框是否已标记

时间:2020-04-04 作者:Jacob Juul Petersen

Updated with working code here:

add_action( \'acf/save_post\', \'fritlaeg_artikel_3m\' );

    function fritlaeg_artikel_3m( $post_id ) {

            if( get_post_meta( $post_id, \'fritlaeg_artikel_i_3_maneder\', true ) ) {

            // Getting a UNIX Timestamp
            $timestamp = time();

            // Using WordPress Time Constants
            // https://codex.wordpress.org/Easier_Expression_of_Time_Constants
            $timestamp_after_hour = $timestamp + 0.5 * MINUTE_IN_SECONDS;

            // Define remaining parameters
            $args = array( $post_id );
            $hook = \'fritlaegning_clear_meta_data\';

            // Get the timestmap of an already scheduled event for the same post and the same event action ($hook)
            // Returns false if it is not scheduled
            $scheduled_timestamp = wp_next_scheduled( $hook, $args );

            if( $scheduled_timestamp == false ) {
                wp_schedule_single_event( $timestamp_after_hour, $hook, $args );
            }

        }

    }

/* Clear the database metadata when cron event fires */

add_action( \'fritlaegning_clear_meta_data\', \'process_clear_meta_data\' );

    function process_clear_meta_data( $post_id ) {

        update_post_meta( $post_id, \'fritlaeg_artikel_i_3_maneder\', \'0\' );

    }

Initial question:

如果在后期编辑页面上标记了复选框,我将尝试运行一个函数。

Scenario: 我已经在所有带有ACF插件的帖子上创建了一个复选框。在我的自定义函数中,我想验证复选框是否已标记,如果这是真的,请运行该函数。

其想法是,选中复选框后,应设置cron事件。一段时间后,cron事件将重置复选框的元数据,从而有效地取消复选框。

以下是我正在使用的函数:

    /* Set up WP cron event on post publish/update */

    add_action( \'publish_post\', \'fritlaeg_artikel_3m\', 10, 2 );

        function fritlaeg_artikel_3m( $post_id, $post ) {

        // Missing if statement here <---   

            // Getting a UNIX Timestamp
            $timestamp = time();

            // Using WordPress Time Constants
            // https://codex.wordpress.org/Easier_Expression_of_Time_Constants
            $timestamp_after_hour = $timestamp + 0.5 * MINUTE_IN_SECONDS;

            // Define remaining parameters
            $args = array( $post_id );
            $hook = \'fritlaegning_clear_meta_data\';

            // Get the timestmap of an already scheduled event for the same post and the same event action ($hook)
            // Returns false if it is not scheduled
            $scheduled_timestamp = wp_next_scheduled( $hook, $args );

            if( $scheduled_timestamp == false ) {
                wp_schedule_single_event( $timestamp_after_hour, $hook, $args );
            }

        } 




    /* Clear the database metadata when cron event fires */

    add_action( \'fritlaegning_clear_meta_data\', \'process_clear_meta_data\' );

        function process_clear_meta_data( $post_id ) {

            update_post_meta( $post_id, \'fritlaeg_artikel_i_3_maneder\', \'0\' );

        }
问题是,无论复选框状态如何,都会在所有已发布的帖子上设置并运行cron事件。

我试着用if (isset($_POST[\'checkbox_name_here\'])) {} 没有任何运气。

选中此框后,以下是编辑帖子页面的html:

enter image description here

<div id="acf-group_5e87343b19c4b" class="postbox  acf-postbox">
<button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Vis eller skjul panel Fritlægning af artikel</span><span class="toggle-indicator" aria-hidden="true"></span></button><h2 class="hndle ui-sortable-handle"><span>Fritlægning af artikel</span><a href="..../wp-admin/post.php?post=517693&amp;action=edit" class="dashicons dashicons-admin-generic acf-hndle-cog acf-js-tooltip" title="Edit field group"></a></h2>
<div class="inside acf-fields -top">
<div class="acf-field acf-field-checkbox acf-field-5e8734782f2b2" data-name="fritlaeg_artikel_i_3_maneder" data-type="checkbox" data-key="field_5e8734782f2b2">
<div class="acf-label">
<label for="acf-field_5e8734782f2b2">Fritlæg artikel i 3 måneder</label></div>
<div class="acf-input">
<input name="acf[field_5e8734782f2b2]" type="hidden">
<ul class="acf-checkbox-list acf-bl">
<li><label class="selected"><input id="acf-field_5e8734782f2b2-Fritlæg-artikel" type="checkbox" name="acf[field_5e8734782f2b2][]" value="Fritlæg artikel" checked="checked">Fritlæg artikel</label></li>
</ul>
</div>
</div>
</div>
</div>
非常感谢您的帮助或建议!

1 个回复
最合适的回答,由SO网友:Jacob Juul Petersen 整理而成

Working code:

add_action( \'acf/save_post\', \'fritlaeg_artikel_3m\' );

    function fritlaeg_artikel_3m( $post_id ) {

            if( get_post_meta( $post_id, \'fritlaeg_artikel_i_3_maneder\', true ) ) {

            // Getting a UNIX Timestamp
            $timestamp = time();

            // Using WordPress Time Constants
            // https://codex.wordpress.org/Easier_Expression_of_Time_Constants
            $timestamp_after_hour = $timestamp + 0.5 * MINUTE_IN_SECONDS;

            // Define remaining parameters
            $args = array( $post_id );
            $hook = \'fritlaegning_clear_meta_data\';

            // Get the timestmap of an already scheduled event for the same post and the same event action ($hook)
            // Returns false if it is not scheduled
            $scheduled_timestamp = wp_next_scheduled( $hook, $args );

            if( $scheduled_timestamp == false ) {
                wp_schedule_single_event( $timestamp_after_hour, $hook, $args );
            }

        }

    }

/* Clear the database metadata when cron event fires */

add_action( \'fritlaegning_clear_meta_data\', \'process_clear_meta_data\' );

    function process_clear_meta_data( $post_id ) {

        update_post_meta( $post_id, \'fritlaeg_artikel_i_3_maneder\', \'0\' );

    }

相关推荐

Posts order in grid

在我的主题中,我有一个显示特色帖子的网格。这张我很喜欢,但帖子的顺序不是我喜欢的:https://i.imgur.com/GzGwRXl.jpg在上面链接的照片上,你可以在红色矩形中看到帖子的当前顺序,下面是绿色矩形,用于说明我想如何显示它们。最大的网格将用于最新发布的帖子。在主题中显示此内容的代码在此处(如果需要,我可以发送包含代码的整个文件):function inner($posts, $td_column_number = \'\') { $buffy = \'\';&#