上下文:我创建了一个热门帖子小部件,它使用元字段作为该帖子受欢迎程度的指标。这个小部件与社交媒体共享插件捆绑在一起,当他们获取流行网络的份额时,它将这些计数存储到元字段中。然后,我们可以根据文章在社交媒体上分享的次数来查询该元字段以显示热门帖子。为什么这个自定义WordPress循环不使用我的参数?
代码示例:这是我正在使用的一小段代码。显然,在这部分代码之后,我们关闭while循环,关闭条件,重置后查询数据等等。这个小部件文件的完整内容可以在GitHub上找到:https://github.com/warfare-plugins/social-warfare/blob/master/functions/widgets.php您看到下面的部分从附近开始该文件的第330行。
// If a custom timeframe is not being used....
if($timeframe == 0):
// Create the arguments for a query without a timeframe
$swp_args = array(
'posts_per_page' => $count,
'post_type' => 'post',
'meta_key' => '_'.$network,
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
// If a custom timeframe is being used....
else:
// Create the arguments for a query with a timeframe
$swp_args = array(
'posts_per_page' => $count,
'post_type' => 'post',
'meta_key' => '_'.$network,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
'column' => 'post_date',
'after' => '- '.$timeframe.' days'
)
);
endif;
// Reset the main query
wp_reset_postdata();
// Query and fetch the posts
$swq = new WP_Query($swp_args);
// Begin the loop
if($swq->have_posts()) :
$i = 1;
while($swq->have_posts()):
$swq->the_post();
问题:我们有刚刚超过5000点这个插件。但是,在少数几个网站上,它不使用我们的自定义查询,而是使用默认的WordPress查询。在这些情况下,它会返回最新的帖子,并根据每页WordPress的帖子设置返回帖子的数量。实际上,例如,在一个网站上,它可以在一个页面上完美显示,但在归档/类别页面上,它会恢复为使用WordPress默认循环而不是我的自定义查询。
我不知道他们是否是另一个插件或主题添加了一些钩子或什么不是WordPress的查询,并重写我的自定义参数。
我迄今试过的唯一的事情是在我的查询之前和之后添加wp_reset_postdata(),但它似乎没有解决任何问题。
问题:有没有什么办法可以运行这个自定义查询,并保证它运行自定义查询并且永远不会运行默认的WordPress查询?
让我引用我在原来的职位说作出回应:“很显然,这部分后,代码,我们关闭while循环,关闭条件,重置后期查询数据等。这个小部件文件的完整内容可以在GitHub上找到:https://github.com/warfare-plugins/social-warfare /blob/master/functions/widgets.php“ –