我正在尝试获取'woocommerce_product_query‘钩子内的WooCommerce主查询的IDS
我试着做这样的事情
add_action( 'woocommerce_product_query', 'custom_get_IDS' );
function custom_get_IDS( $q ) {
if($q->have_posts()){
while($q->have_posts()){
$q->the_post();
//get the id
echo 'print the ID';
}
} else {
echo 'no posts';
}
}但是这个打印总是“无帖子”,即使页面循环有2个产品。
如果我这样做了
print_r($q);我得到了具有所有查询设置的WP_Query对象,例如
[query] => Array
(
[s] => my_query_word
[post_type] => product
[lang] => en
)
[query_vars] => Array
(
[s] => my_query_word
[post_type] => product
[lang] => en
[error] =>
[m] =>
[p] => 0
[post_parent] =>
etc etc etc如何在woocommerce_product_query钩子中获取$q查询的I?
我必须在woocommerce_product_query钩子中执行此操作。
发布于 2020-05-06 04:16:32
不推荐这样做,但这可能会得到您想要的输出。
add_action( 'woocommerce_product_query', 'custom_get_IDS' );
function custom_get_IDS( $q ) {
remove_action( 'woocommerce_product_query', 'custom_get_IDS' );
$posts = $q->get_posts();
if ( $posts ) {
foreach ( $posts as $post ) {
//get the id
echo 'print the ID=' . $post->ID;
}
} else {
echo 'no posts';
}
}https://stackoverflow.com/questions/61610299
复制相似问题