繁体   English   中英

Wordpress WP_Query / get_posts其中post_title等于返回所有结果

[英]Wordpress WP_Query/get_posts where post_title equals returns all results

我创建了一个名为“snippets”的自定义帖子类型,其中有一些数据片段,例如客户端可以更改的“地址”。

片段的标题都是独一无二的: 自定义帖子类型

我已经创建了一个简单的函数来返回这些片段,但它不能正常工作,我不知道为什么。

功能:

function get_snippet($snippet_title) {
    $snippet_page = new WP_Query(array(
        'post_type' => 'snippets',
        'post_title' => $snippet_title
    ));
    return $snippet_page->posts[0]->post_content;
}

以下是对函数的示例调用:

echo get_snippet('footer_address');

问题是:

它将始终返回所有片段,而不是按post_title过滤。

即使我使用get_posts()它也会以相同的顺序返回所有片段,并且不会返回基于post_title的单个片段。

结果相当于:

$snippet_page = new WP_Query(array(
    'post_type' => 'snippets'
));

为什么不通过'post_title'过滤?

谢谢

(我也使用多站点)

这是我的自定义帖子类型初始化代码:

function snippets() {
    $labels = array(
        'name'                => _x( 'Snippets', 'Post Type General Name', 'text_domain' ),
        'singular_name'       => _x( 'Snippet', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'           => __( 'Snippets', 'text_domain' ),
        'name_admin_bar'      => __( 'Snippets', 'text_domain' ),
        'parent_item_colon'   => __( 'Parent Item:', 'text_domain' ),
        'all_items'           => __( 'All Snippets', 'text_domain' ),
        'add_new_item'        => __( 'Add New Snippet', 'text_domain' ),
        'add_new'             => __( 'Add New Snippet', 'text_domain' ),
        'new_item'            => __( 'New Snippet', 'text_domain' ),
        'edit_item'           => __( 'Edit Snippet', 'text_domain' ),
        'update_item'         => __( 'Update Snippet', 'text_domain' ),
        'view_item'           => __( 'View Snippet', 'text_domain' ),
        'search_items'        => __( 'Search Snippet', 'text_domain' ),
        'not_found'           => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'text_domain' ),
    );
    $args = array(
        'label'               => __( 'snippets', 'text_domain' ),
        'description'         => __( 'For custom snippets of code/data.', 'text_domain' ),
        'labels'              => $labels,
        'supports'            => array( ),
        'hierarchical'        => false,
        'public'              => false,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'menu_icon'           => 'dashicons-tagcloud',
        'menu_position'       => 5,
        'show_in_admin_bar'   => true,
        'show_in_nav_menus'   => false,
        'can_export'          => true,
        'has_archive'         => true,      
        'exclude_from_search' => true,
        'publicly_queryable'  => false,
        'capability_type'     => 'page',
    );
    register_post_type( 'snippets', $args );
}
add_action( 'init', 'snippets', 0 );

我知道这是一个非常古老的帖子,但也许有人会像我今天一样寻找解决方案。

事实证明,你只需要使用'title'而不是'post_title'。

这是一个例子:

$author_id = 20;
$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_status' => 'inherit',
    'title' => 'Reviewer Plugin - User review image',
    'author' => $author_id,
); 
$attachments = get_posts($args);
if ($attachments) {
    foreach ($attachments as $attachedfile) {
        $mediaIds =  $attachedfile->ID .','. $mediaIds;
    }
    $mediaIds = rtrim($mediaIds,",");
} 
print_r($mediaIds);

如果仍然无法获得任何解决方案,请尝试以下手动查询:

global $wpdb; $res = $wpdb->get_results("SELECT * FROM wp_posts where post_type='snippet' AND post_title='".$snippet_title."'");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM