为什么我的自定义帖子类型会随着时间的推移停止显示?

时间:2020-03-15 作者:Dawn Summerall

我在这里有一个技能发展项目:https://blog.unicornpoint.net/wp/safe-foods-meal-generator-diet-assistant/ . 这是一个自定义模板页面。我使用自定义贴子UI来定制贴子类型的膳食,使用ACF来定制一些字段。随着时间的推移,一些食物不再显示在页面上。我在底部列出了膳食,具体取决于膳食的大小和膳食组,当您单击按钮时,这些膳食组将使用jquery、ajax和php显示。这两种方法都会在一段时间后停止显示某些餐点类型。它的类型或大小并不总是相同的。这是某种缓存吗?我仍在学习这一切,我需要一些帮助。

Jquery脚本

    <div class="mx-auto meal-icons-container">
        <div class="row p-2 mx-auto">
            <div class="col-6 col-md-3">
                <img src="https://blog.unicornpoint.net/wp/wp-content/uploads/2020/02/panic.png" id="emergency-icon" width="75" height="auto" alt="Emergency Meals" class="meal-icon"><span class="meal-icon-caption"> Emergency</span>
            </div>
            <script>
                jQuery(document).ready(function($){
                    $(\'#emergency-icon\').click(function(){
                        $.ajax({
                            method: \'GET\',
                url: ajaxurl, // we send all WP ajax requests to this file, it will load all WP\'s files, plugins. It\'s WP Ajax approach.
                data: {
                  action: \'load_emergency_meals\' // you can name it whatever just use the same in PHP binding ajax action.
              }
          }).done(function(data){
            $("#meal-results").html(data);
          });
      });
                });
            </script>
函数中的函数。php

// load emergency meals button
function load_emergency_meals() {
 ?>
    <h2>
        Emergency Binge Control Meals
    </h2>
<div class="justify-content-center d-inline-flex flex-wrap text-center p-2">
    <?php
    $args = array(\'post_type\' => \'meal\');
    $meals = new WP_Query($args);

    if($meals->have_posts()) : while($meals->have_posts()) : $meals->the_post();
        $meal_type = get_field(\'meal_type\');
        if ( $meal_type && in_array(\'emergency\', $meal_type)){?>
            <div class="text-center p-2 meal-size-posts">
                <h4><?php the_field(\'meal_name\'); ?></h4>
                <img src="<?php echo get_field(\'meal_icon\'); ?>" class="meal-icon">
                <p class="meal-description"><?php the_field(\'meal_description\'); ?></p>
                <h4><?php the_field(\'meal_calories\'); ?> calories</h4>
            </div>
        <?php } ?>
    <?php endwhile; endif; ?>
</div>
<?php
    die();
}
功能中的操作。php

 // this is where we bind our function to `load_meals` action name.
 // the one with `_nopriv` prefix in wp action name is for not logged in users, and without it for logged in users.
add_action(\'wp_ajax_load_emergency_meals\', \'load_emergency_meals\');
add_action(\'wp_ajax_nopriv_load_emergency_meals\', \'load_emergency_meals\');
自定义帖子类型设置https://blog.unicornpoint.net/wp/wp-content/uploads/2020/03/meals-cpt-settings.jpg

自定义字段设置

if( function_exists(\'acf_add_local_field_group\') ):

acf_add_local_field_group(array(
    \'key\' => \'group_5e590c435c453\',
    \'title\' => \'Meal\',
    \'fields\' => array(
        array(
            \'key\' => \'field_5e591fd27bd3f\',
            \'label\' => \'Name\',
            \'name\' => \'meal_name\',
            \'type\' => \'text\',
            \'instructions\' => \'What is this meal called?\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'default_value\' => \'\',
            \'placeholder\' => \'name the meal\',
            \'prepend\' => \'\',
            \'append\' => \'\',
            \'maxlength\' => \'\',
        ),
        array(
            \'key\' => \'field_5e5920a99f373\',
            \'label\' => \'Meal Icon\',
            \'name\' => \'meal_icon\',
            \'type\' => \'image\',
            \'instructions\' => \'Choose an icon for this food.\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'return_format\' => \'url\',
            \'preview_size\' => \'medium\',
            \'library\' => \'all\',
            \'min_width\' => \'\',
            \'min_height\' => \'\',
            \'min_size\' => \'\',
            \'max_width\' => \'\',
            \'max_height\' => \'\',
            \'max_size\' => \'\',
            \'mime_types\' => \'\',
        ),
        array(
            \'key\' => \'field_5e592164996b4\',
            \'label\' => \'Meal Description\',
            \'name\' => \'meal_description\',
            \'type\' => \'textarea\',
            \'instructions\' => \'Describe the meal.\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'default_value\' => \'\',
            \'placeholder\' => \'What foods are in this meal and what\\\'s the calorie breakdown?\',
            \'maxlength\' => \'\',
            \'rows\' => \'\',
            \'new_lines\' => \'\',
        ),
        array(
            \'key\' => \'field_5e590c7a69cc7\',
            \'label\' => \'Calories\',
            \'name\' => \'meal_calories\',
            \'type\' => \'number\',
            \'instructions\' => \'Add how many calories this meal contains.\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'default_value\' => \'\',
            \'placeholder\' => \'add calories\',
            \'prepend\' => \'\',
            \'append\' => \'\',
            \'min\' => \'\',
            \'max\' => \'\',
            \'step\' => \'\',
        ),
        array(
            \'key\' => \'field_5e6e03dd8dee2\',
            \'label\' => \'Meal Type\',
            \'name\' => \'meal_type\',
            \'type\' => \'checkbox\',
            \'instructions\' => \'\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'choices\' => array(
                \'emergency\' => \'emergency\',
                \'basic\' => \'basic\',
                \'alcoholic\' => \'alcoholic\',
                \'asian\' => \'asian\',
                \'mexican\' => \'mexican\',
                \'comfort\' => \'comfort\',
                \'cooling\' => \'cooling\',
                \'sweet\' => \'sweet\',
            ),
            \'allow_custom\' => 0,
            \'default_value\' => array(
            ),
            \'layout\' => \'vertical\',
            \'toggle\' => 0,
            \'return_format\' => \'value\',
            \'save_custom\' => 0,
        ),
        array(
            \'key\' => \'field_5e6e04b63a353\',
            \'label\' => \'Meal Size\',
            \'name\' => \'meal_size\',
            \'type\' => \'select\',
            \'instructions\' => \'\',
            \'required\' => 1,
            \'conditional_logic\' => 0,
            \'wrapper\' => array(
                \'width\' => \'\',
                \'class\' => \'\',
                \'id\' => \'\',
            ),
            \'choices\' => array(
                \'small\' => \'small\',
                \'medium\' => \'medium\',
                \'large\' => \'large\',
                \'xl\' => \'xl\',
            ),
            \'default_value\' => array(
            ),
            \'allow_null\' => 0,
            \'multiple\' => 0,
            \'ui\' => 0,
            \'return_format\' => \'value\',
            \'ajax\' => 0,
            \'placeholder\' => \'\',
        ),
    ),
    \'location\' => array(
        array(
            array(
                \'param\' => \'post_type\',
                \'operator\' => \'==\',
                \'value\' => \'meal\',
            ),
        ),
    ),
    \'menu_order\' => 0,
    \'position\' => \'normal\',
    \'style\' => \'default\',
    \'label_placement\' => \'top\',
    \'instruction_placement\' => \'label\',
    \'hide_on_screen\' => array(
        0 => \'the_content\',
        1 => \'featured_image\',
    ),
    \'active\' => true,
    \'description\' => \'\',
));

endif;
这可能是什么问题?感谢您的帮助。谢谢

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

那为什么呢

因为您的WP_Query/query是查询您最近发布的帖子类型(针对所有用餐类型),因此结果可能会包含以下帖子:meal_type 元包含emergency 作为价值的一部分。

然而,您下面的代码仅在帖子属于指定的用餐类型时显示帖子,即emergency 在代码中:

$meal_type = get_field(\'meal_type\');
if ( $meal_type && in_array(\'emergency\', $meal_type))

解决方案

$args:

$args = array(
    \'post_type\'  => \'meal\',
    \'meta_query\' => array(
        array(
            \'key\'     => \'meal_type\',
            \'value\'   => \'"emergency"\',
            \'compare\' => \'LIKE\',
        ),
    ),
);
A bit of explanation: 在上面的元查询中,我们使用LIKE 子句,因为元值是一个具有序列化字符串的数组a:1:{i:0;s:9:"emergency";} 在数据库中,因此我们使用了精确匹配&mdash;"<the type>". 它应该与您当前的ACF设置配合使用,但您需要调整value 相应的其他膳食类型。E、 g.对于“基本”膳食类型,您可以使用\'value\' => \'"basic"\' (注意引号/" 是必要的)。

更好的做法是创建一个名为meal_type 并将其分配给post类型(meal). 或者我可以使用默认类别(category) 分类学但无论哪种方式,我都不需要使用上面的元查询,或者通过类别/分类法进行查询更容易,分类法就是用来进行分类的。因此,在创建更多帖子之前,您应该考虑这一点。。

相关推荐