所以,我正在尝试在我正在构建的网站中实现一些自定义逻辑,这要求我创建一个自定义帖子类型,我查询显示在woocommerce产品页面上。
我使用以下函数进行查询。
function get_mod ( $post_name, $post_type = 'productmods' )
{
$post_ids = new WP_Query(array
(
'name' => sanitize_title($post_name),
'post_type' => $post_type
));
return $post_ids;
}
此功能适用于除safari之外的所有浏览器。当我在safari中查看页面时,我将vardump输出到查询对象的页面上。它会根据我使用的浏览器返回不同的结果。以下是截图:
有没有人听说过这种情况?我已经尝试谷歌搜索它,但是,我只是问题非特定于它发生在哪个浏览器。并且我尝试过的所有解决方案都不起作用。
任何帮助将不胜感激。谢谢!
根据评论。以下是safari和chrome上输出的明文:
WP_Query Object
(
[query] => Array
(
[name] => signature-flavors-select-one-if-you-chose-signature-pricing
[post_type] => productmods
)
[query_vars] => Array
(
[name] => signature-flavors-select-one-if-you-chose-signature-pricing
[post_type] => productmods
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[posts_per_page] => 10
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] =>
[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
)
[relation] =>
[meta_table] =>
[meta_id_column] =>
[primary_table] =>
[primary_id_column] =>
[table_aliases:protected] => Array
(
)
[clauses:protected] => Array
(
)
[has_or_relation:protected] =>
)
[date_query] =>
[request] => SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'signature-flavors-select-one-if-you-chose-signature-pricing' AND wp_posts.post_type = 'productmods' ORDER BY wp_posts.post_date DESC
[posts] => Array
(
)
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 1
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => 1
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] => 1
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => 1f637d9a855417c5b7cd26de9027165b
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
WP_Query Object
(
[query] => Array
(
[name] => signature-flavors-select-one-if-you-chose-signature-pricing
[post_type] => productmods
)
[query_vars] => Array
(
[name] => signature-flavors-select-one-if-you-chose-signature-pricing
[post_type] => productmods
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
[ignore_sticky_posts] =>
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[lazy_load_term_meta] => 1
[update_post_meta_cache] => 1
[posts_per_page] => 10
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)
[tax_query] =>
[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
)
[relation] =>
[meta_table] =>
[meta_id_column] =>
[primary_table] =>
[primary_id_column] =>
[table_aliases:protected] => Array
(
)
[clauses:protected] => Array
(
)
[has_or_relation:protected] =>
)
[date_query] =>
[request] => SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = 'signature-flavors-select-one-if-you-chose-signature-pricing' AND wp_posts.post_type = 'productmods' ORDER BY wp_posts.post_date DESC
[posts] => Array
(
[0] => WP_Post Object
(
[ID] => 1294
[post_author] => 1
[post_date] => 2018-02-23 17:19:37
[post_date_gmt] => 0000-00-00 00:00:00
[post_content] => NONE- I selected CLASSIC above|Devils food, fudge and salted caramel|Devils Food, Ganache, Cherry-Vanilla buttercream|Devils food, fudge, graham crumble, marshmallow fluff bc|Almond Cake, Apricot jam, Amaretto buttercream|Coconut cake, passion curd, coconut milk bc|Satisfy the Hunger - devils, snickers caramel, peanut butter bc|Biscoff Cake, Cookie Butter, Cookie Buttercream|Elderflower Soaked Lemon Cake - blackberry jam, vanilla buttercream|Apple Cake, Pear Butter, Caramel Buttercream
[post_title] => SIGNATURE FLAVORS (select ONE if you chose "signature" pricing)
[post_excerpt] =>
[post_status] => draft
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => signature-flavors-select-one-if-you-chose-signature-pricing
[to_ping] =>
[pinged] =>
[post_modified] => 2018-01-25 16:22:10
[post_modified_gmt] => 2018-01-25 16:22:10
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://www.leadingdc.com/buttercream/?post_type=productmods&p=1294
[menu_order] => 0
[post_type] => productmods
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
[post_count] => 1
[current_post] => -1
[in_the_loop] =>
[post] => WP_Post Object
(
[ID] => 1294
[post_author] => 1
[post_date] => 2018-02-23 17:19:37
[post_date_gmt] => 0000-00-00 00:00:00
[post_content] => NONE- I selected CLASSIC above|Devils food, fudge and salted caramel|Devils Food, Ganache, Cherry-Vanilla buttercream|Devils food, fudge, graham crumble, marshmallow fluff bc|Almond Cake, Apricot jam, Amaretto buttercream|Coconut cake, passion curd, coconut milk bc|Satisfy the Hunger - devils, snickers caramel, peanut butter bc|Biscoff Cake, Cookie Butter, Cookie Buttercream|Elderflower Soaked Lemon Cake - blackberry jam, vanilla buttercream|Apple Cake, Pear Butter, Caramel Buttercream
[post_title] => SIGNATURE FLAVORS (select ONE if you chose "signature" pricing)
[post_excerpt] =>
[post_status] => draft
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => signature-flavors-select-one-if-you-chose-signature-pricing
[to_ping] =>
[pinged] =>
[post_modified] => 2018-01-25 16:22:10
[post_modified_gmt] => 2018-01-25 16:22:10
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://www.leadingdc.com/buttercream/?post_type=productmods&p=1294
[menu_order] => 0
[post_type] => productmods
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[comment_count] => 0
[current_comment] => -1
[found_posts] => 1
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] => 1
[is_preview] => 1
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] => 1
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash:WP_Query:private] => 1f637d9a855417c5b7cd26de9027165b
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
根据评论,这里是调用get_mod
函数的函数。
function add_modifier_fields() {
$fields = array();
$mods = get_post_meta(get_the_ID(), 'square_modifiers', true ); //retrieves a custom field from a product in the db. This has been confirmed to return correctly.
$mods = explode("|", $mods);
foreach( $mods as $key => $mod ):
//this loop also runs as expected, which is why I am retrieving 2 outputs of the $post object.
$post = get_mod($mod); //$mod is the name (str) of a category which maps to a "productmod".
echo "<pre>".print_r($post, true)."</pre>";
if($post -> have_posts()):
//this is where it fails!!! don't know why it is not working.
$_mod = $post -> get_posts();
$match = preg_match('/CLASSIC FLAVORS|SIGNATURE FLAVORS/', $mod, $name);
echo "<h3>I'm here! </h3>";
...
}