我正在编写一个WordPress插件,用于创建短代码。在该插件的设置页面上,我想列出使用该短代码的所有页面。(为了方便访问和查找以进行快速更改。)
我觉得可以使用get_post(),其中它搜索所有与过滤器匹配的帖子,然后列出它们,但我似乎无法弄清楚语法是什么,或者是否可能。
有什么建议吗?
我觉得可以使用get_post(),其中它搜索所有与过滤器匹配的帖子,然后列出它们,但我似乎无法弄清楚语法是什么,或者是否可能。
有什么建议吗?
在 WordPress 论坛的帮助下,我找到了解决方法。
成功地按照以下方式完成:
global $wpdb;
$query = "SELECT ID, post_title, guid FROM ".$wpdb->posts." WHERE post_content LIKE '%[mod-%' AND post_status = 'publish'";
$results = $wpdb->get_results ($query);
然后像这样显示:
<?php foreach ( $results as $results ) { ?><p><a>ID;?>"><?php echo $results->post_title;?></a><br></p><?php } ?>
add_shortcode('find_posts_with_shortcode', function ($atts = []) {
$atts = wp_parse_args($atts, [
'tag' => false,
]);
if (!$atts['tag']) {
return __('"tag" parameter is required');
}
$tag = trim($atts['tag']);
// Adapted from shortcodes.php#add_shortcode
if (0 !== preg_match('@[<>&/\[\]\x00-\x20=]@', $tag)) {
/* translators: 1: Shortcode name, 2: Space-separated list of reserved characters. */
return sprintf(__('Invalid shortcode name: %1$s. Do not use spaces or reserved characters: %2$s'), $tag, '& / < > [ ] =');
}
global $wpdb;
$query = $wpdb->prepare(
"SELECT id, post_title, post_status
FROM $wpdb->posts
WHERE (post_content LIKE %s OR post_content LIKE %s)
AND post_status IN ('publish', 'private', 'draft')
ORDER BY post_title",
'%[' . $wpdb->esc_like($tag) . ' %', // i.e. [tag param..]
'%[' . $wpdb->esc_like($tag) . ']%' // i.e. [tag]
);
$shortcode_usages = $wpdb->get_results($query);
if (count($shortcode_usages) == 0) {
return __('No usage found');
}
ob_start();
foreach ($shortcode_usages as $post) {
echo '<a href="' . get_permalink($post->id) . '">' . $post->post_title . ' (' . $post->post_status . ')</a><br />';
}
return ob_get_clean();
});
strpos
。 或者,您可以直接查询DB:SELECT * FROM
wp_postmetaWHERE
meta_valueLIKE '%[hello]%' LIMIT 0,1000
; 这将为您提供包括post_ids在内的结果集。 - bakuwp_posts
WHEREpost_content
LIKE '%[mod-%' ANDpost_status
= 'publish' LIMIT 0,1000 来实现一个系统,这是基于 @baku 的评论。将会发布结果。 - Riley Magnuson