WP PARSE_QUERY不使用自定义元值

时间:2018-05-12 作者:S.k.joy

我有一个自定义的帖子类型和一些自定义的元框。我想使用自定义元值筛选自定义帖子类型的帖子。我已经编写了下面的代码。但它没有返回找到的帖子。谁能告诉我哪里做错了?这是代码。

<?php

add_action(\'restrict_manage_posts\',\'restrict_listings_by_metavalue\');
function restrict_listings_by_metavalue() {
    global $typenow;
    global $wp_query;
    if ($typenow==\'jspp_std\') {
        echo \'<input type="text" name="adate" id="adate" placeholder="Enter Admission date" />\';
    }
}
add_filter(\'parse_query\',\'jspp_custom_meta_query\');
function jspp_custom_meta_query($query) {
    global $typenow;
    global $pagenow;

    if( $pagenow == \'edit.php\' && $typenow == \'jspp_std\' && isset($_GET[\'adate\']) )
    {

        $query->query_vars[\'meta_key\'] = \'_jspp_sp_sid\';
        $query->query_vars[\'meta_value\'] = $_GET[\'adate\'];
        $query->query_vars[\'meta_compare\'] = \'=\';

    }
}

?>
返回结果的屏幕截图。提前谢谢。

enter image description here

2 个回复
SO网友:Ejaz UL Haq

当我试图用自定义字段过滤自定义帖子类型时,我遇到了同样的问题,但我已经在以下步骤中解决了这个问题
已从转换的自定义字段名writer_writer
然后我更新了以下代码callback function 属于parse_query 我可以添加自定义字段meta\\u查询的挂钩,如

$query->set( \'meta_query\', array(
    array(
          \'key\'     => \'_writer\',
           \'compare\' => \'=\',
           \'value\'   => $_GET[\'_writer\'],
           \'type\'    => \'numeric\',
      )
) );         
This Above Solution 为我工作
Documentation https://developer.wordpress.org/reference/hooks/pre_get_posts/
Helpful Links https://developer.wordpress.org/reference/hooks/pre_get_posts/#comment-2571
https://stackoverflow.com/questions/47869905/how-can-i-filter-records-in-custom-post-type-list-in-admin-based-on-user-id-that

SO网友:Ellis Benus Web Developer

UPDATE: I figured it out! My fix was I could only filter by Columns which were specified to be sortable. Hopefully this helps someone else!

因此,对于我的自定义帖子类型,我只有一个列设置为可排序。我添加了我试图筛选的专栏,然后砰的一声!过滤成功!

// Make Custom Post Type Custom Columns Sortable
function cpt_custom_columns_sortable( $columns ) {

    // Add our columns to $columns array
    $columns[\'item_number\'] = \'item_number\';
    $columns[\'coat_school\'] = \'coat_school\'; 

    return $columns;
} add_filter( \'manage_edit-your-custom-post-type-slug_sortable_columns\', \'cpt_custom_columns_sortable\' );
因此,包括上述内容,我的整个代码解决方案如下所示:

/**
 * Add School as a Filter Drop Menu
 * Create the dropdown
 * Change POST_TYPE to the name of your custom post type
 */
function ssp_school_custom_column_filter() {

    // Only show the filter drop menu to Admins/SSP Users
    $user = wp_get_current_user();
    $role = ( array ) $user->roles;
    if ( in_array("administrator", $role ) ||  in_array("ssp_user", $role ) ) {
        // do nothing - this is backwards... shouldn\'t need to do it this way.
    } else { return; }

    $type = \'post\'; // Set default to be overridden

    // Check for the Post Type
    if (isset($_GET[\'post_type\'])) {
        $type = $_GET[\'post_type\'];
    }

    $types = array( \'member\' );
    $types_uniforms = ssp_get_uniform_items_names_by_size(\'all\');   
    $types = array_merge( $types , $types_uniforms );

    // Only add filter to certain post types
    if ( in_array( $type , $types) ) {

        // Get all Schools
        $schools_args = array(
            \'post_type\' => \'schools\',
            \'post_status\'   => \'publish\',
            \'posts_per_page\'=> \'-1\',
            \'orderby\'   => \'title\',
            \'order\'     => \'ASC\',
            \'fields\'    => \'ids\',
        );
        $schools = new WP_Query( $schools_args );

        // Populate the List of Schools
        if ( $schools->have_posts() ) {
            $values = array();
            while ( $schools->have_posts() ) {
                $schools->the_post();
                $values += [ get_the_title() => get_the_ID() ];
            }
        } else {
            $values = array(
                \'Error: No Schools Available\' => \'error\',
            );
        }
        ?>
        <select name="ssp_school_custom_column_filter">
        <option value="">Filter by School</option>
        <?php
            $current_school = isset($_GET[\'ssp_school_custom_column_filter\']) ? $_GET[\'ssp_school_custom_column_filter\'] : \'\';
            foreach ($values as $label => $value) {
                printf (
                    \'<option value="%s" %s>%s</option>\',
                    $value,
                    $value == $current_school ? \'selected="selected"\' : \'\',
                    $label
                );
            }
        ?>
        </select>
        <?php
    }
} add_action( \'restrict_manage_posts\', \'ssp_school_custom_column_filter\' );

/**
 * Handle Submitted School Filter
 *
 * Change META_KEY to the actual meta key
 * and POST_TYPE to the name of your custom post type
 *
 * @param  (wp_query object) $query
 *
 * @return Void
 */
function ssp_school_custom_column_posts_filter( $query ){

    global $pagenow;

    $type = \'post\';
    if (isset($_GET[\'post_type\'])) {
        $type = $_GET[\'post_type\'];
    }

    // Manually add members b/c they aren\'t Uniform Items
    $types = array( \'member\' );
    // Get all the Uniform Items and add them to Types
    $types_uniforms = ssp_get_uniform_items_names_by_size(\'all\');   
    // Combine Members and Uniform Items
    $types = array_merge( $types , $types_uniforms );

    if ( in_array( $type , $types) &&
         is_admin() &&
         $pagenow == \'edit.php\' &&
         isset($_GET[\'ssp_school_custom_column_filter\']) &&
         $_GET[\'ssp_school_custom_column_filter\'] != \'\' &&
         $query->is_main_query()
        )
    {
        // Get the post type object
        $type_obj = get_post_type_object( $type );

        // The meta_key is dependent on the post type
        $query->query_vars[\'meta_key\'] = \'assigned_school\';
        $query->query_vars[\'meta_value\'] = intval($_GET[\'ssp_school_custom_column_filter\']);
        $query->query_vars[\'meta_compare\'] = \'=\';
    }

} add_filter( \'parse_query\', \'ssp_school_custom_column_posts_filter\' );
我也有同样的问题。

我发现的所有代码示例如下所示,我们的代码(你的和我的)似乎都与这些示例一样,但似乎都不起作用

Filter by custom field in custom post type on admin page

结束

相关推荐