为媒体上载程序创建自定义查询时出现循环问题

时间:2012-08-08 作者:Philip Downer

我正在为一家杂志出版商写一个主题,他经常处理大量的图片。我需要他们能够将每个图像与贡献者(摄影师、插画家、艺术家等)关联起来

每个贡献者都包含在一个自定义帖子类型“jdp\\U贡献者”中,贡献者类型是一个自定义分类法“jdp\\U贡献者\\U类型”。

使用WordPress挂钩“attachment\\u fields\\u to\\u edit”,我想添加一个选择菜单,允许用户为图像选择贡献者。我让网站弹出菜单并正确保存值,但仍有一些奇怪的错误。

1) 通过媒体上传器添加新图像时,可以从(50多个)贡献者的完整列表中进行选择。

2) 通过“媒体上传器”(在“媒体库”选项卡中)编辑图像时,只有前10个贡献者可供选择。

要创建选择菜单,我正在创建一个WP\\u QUERY的新实例。WP\\u QUERY的参数状态为“posts\\u per\\u page”=-1和“nopage”设置为true。

我不知道我做得不对。

<?php
/**
* Add Photographer selection to media upload
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
function bsj_attachment_field_credit( $form_fields, $post ) {
    //do_dump($post);

    $form_fields[\'jdp_photographer\'] = array(
        \'label\' => \'Contributor:\',
        \'input\' => \'html\',
        \'helps\' => \'Choose the photographer/artist for this image\',
    );

    //GET THE CURRENT PHOTOGRAPHER
    $current_photog = get_post_meta($post->ID, \'jdp_photographer\', true);
    if ( !isset($current_photog) ) {
        $current_photog = 0;    
    }

    //SETUP THE FORM FIELD
    $form_fields[\'jdp_photographer\'][\'html\'] = \'<select name="attachments[\'.$post->ID.\'][jdp_photographer]" id="attachments[\'.$post->ID.\'][jdp_photographer]">\';

    //OUTPUT NULL FIELD
    $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="0" \'.selected(0, $current_photog, false).\'>Choose one...</option>\';

    //GET THE TERMS IN THE \'jdp_contributor_types\' taxonomy, excluding authors
    $terms = get_terms( \'jdp_contributor_types\', array(\'fields\' => \'ids\', \'hide_empty\' => false, \'exclude\' => \'32\') );

    //GET THE PHOTOGRAPHERS
    $args = array(
        \'post_type\' => \'jdp_contributors\',
        \'nopaging\' => true,
        \'posts_per_page\' => -1,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'jdp_contributor_types\',
                \'field\' => \'id\',
                \'terms\' => $terms
            ),
        ),
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
    );
    $photographers = new WP_QUERY($args);

    //LOOP THRU THE PHOTOGRAPHERS
    foreach( $photographers as $postdata ) {
        setup_postdata($postdata);
        $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="\'.$postdata->ID.\'" \'.selected($postdata->ID, $current_photog, false).\'>\'.$postdata->post_title.\'</option>\';
    }
    wp_reset_query();

    $form_fields[\'jdp_photographer\'][\'html\'] .= \'</select>\';

    return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'bsj_attachment_field_credit\', 10, 2 );


/**
* Save values of Photographer Name and URL in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function jdp_attachment_field_credit_save( $post, $attachment ) {
    if (isset( $attachment[\'jdp_photographer\'] ) )
        update_post_meta( $post[\'ID\'], \'jdp_photographer\', $attachment[\'jdp_photographer\'] );

    return $post;
}
add_filter( \'attachment_fields_to_save\', \'jdp_attachment_field_credit_save\', 10, 2 );
?>

1 个回复
最合适的回答,由SO网友:Philip Downer 整理而成

我不完全清楚为什么get\\u posts()函数可以工作,而不是创建WP\\u查询的新实例。根据我对WordPress查询工作原理的理解,get\\u posts()实际上是在创建自己的WP\\u QUERY实例。

不管怎样,这是我目前使用的解决方案。

<?php
    //GET THE PHOTOGRAPHERS
    $args = array(
        \'post_type\' => \'jdp_contributors\',
        \'numberposts\' => -1,
        \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'jdp_contributor_types\',
                        \'field\' => \'id\',
                        \'terms\' => $terms
                    ),
        )
    );
    $photographers = get_posts($args);

    //LOOP THRU THE PHOTOGRAPHERS
    foreach( $photographers as $post ) {
        $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="\'.$post->ID.\'" \'.selected($post->ID, $current_photog, false).\'>\'.$post->post_title.\'</option>\';
    }
?>

结束

相关推荐

How to set up pagination

这听起来很简单,但我发现它不可能实现。这是我的website, 你可以在主页上看到文章摘要,但我只显示了大约25篇文章中的15篇,通过使用下面代码的“获取文章”插件。我想使用分页在不同的页面上显示其余内容。有什么想法吗?[tpg_get_posts category_name=\"archives\" numberposts=\"15\" shorten_content=\"w790\" ]

为媒体上载程序创建自定义查询时出现循环问题 - 小码农CODE - 行之有效找到问题解决它

为媒体上载程序创建自定义查询时出现循环问题

时间:2012-08-08 作者:Philip Downer

我正在为一家杂志出版商写一个主题,他经常处理大量的图片。我需要他们能够将每个图像与贡献者(摄影师、插画家、艺术家等)关联起来

每个贡献者都包含在一个自定义帖子类型“jdp\\U贡献者”中,贡献者类型是一个自定义分类法“jdp\\U贡献者\\U类型”。

使用WordPress挂钩“attachment\\u fields\\u to\\u edit”,我想添加一个选择菜单,允许用户为图像选择贡献者。我让网站弹出菜单并正确保存值,但仍有一些奇怪的错误。

1) 通过媒体上传器添加新图像时,可以从(50多个)贡献者的完整列表中进行选择。

2) 通过“媒体上传器”(在“媒体库”选项卡中)编辑图像时,只有前10个贡献者可供选择。

要创建选择菜单,我正在创建一个WP\\u QUERY的新实例。WP\\u QUERY的参数状态为“posts\\u per\\u page”=-1和“nopage”设置为true。

我不知道我做得不对。

<?php
/**
* Add Photographer selection to media upload
*
* @param $form_fields array, fields to include in attachment form
* @param $post object, attachment record in database
* @return $form_fields, modified form fields
*/
function bsj_attachment_field_credit( $form_fields, $post ) {
    //do_dump($post);

    $form_fields[\'jdp_photographer\'] = array(
        \'label\' => \'Contributor:\',
        \'input\' => \'html\',
        \'helps\' => \'Choose the photographer/artist for this image\',
    );

    //GET THE CURRENT PHOTOGRAPHER
    $current_photog = get_post_meta($post->ID, \'jdp_photographer\', true);
    if ( !isset($current_photog) ) {
        $current_photog = 0;    
    }

    //SETUP THE FORM FIELD
    $form_fields[\'jdp_photographer\'][\'html\'] = \'<select name="attachments[\'.$post->ID.\'][jdp_photographer]" id="attachments[\'.$post->ID.\'][jdp_photographer]">\';

    //OUTPUT NULL FIELD
    $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="0" \'.selected(0, $current_photog, false).\'>Choose one...</option>\';

    //GET THE TERMS IN THE \'jdp_contributor_types\' taxonomy, excluding authors
    $terms = get_terms( \'jdp_contributor_types\', array(\'fields\' => \'ids\', \'hide_empty\' => false, \'exclude\' => \'32\') );

    //GET THE PHOTOGRAPHERS
    $args = array(
        \'post_type\' => \'jdp_contributors\',
        \'nopaging\' => true,
        \'posts_per_page\' => -1,
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'jdp_contributor_types\',
                \'field\' => \'id\',
                \'terms\' => $terms
            ),
        ),
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
    );
    $photographers = new WP_QUERY($args);

    //LOOP THRU THE PHOTOGRAPHERS
    foreach( $photographers as $postdata ) {
        setup_postdata($postdata);
        $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="\'.$postdata->ID.\'" \'.selected($postdata->ID, $current_photog, false).\'>\'.$postdata->post_title.\'</option>\';
    }
    wp_reset_query();

    $form_fields[\'jdp_photographer\'][\'html\'] .= \'</select>\';

    return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'bsj_attachment_field_credit\', 10, 2 );


/**
* Save values of Photographer Name and URL in media uploader
*
* @param $post array, the post data for database
* @param $attachment array, attachment fields from $_POST form
* @return $post array, modified post data
*/
function jdp_attachment_field_credit_save( $post, $attachment ) {
    if (isset( $attachment[\'jdp_photographer\'] ) )
        update_post_meta( $post[\'ID\'], \'jdp_photographer\', $attachment[\'jdp_photographer\'] );

    return $post;
}
add_filter( \'attachment_fields_to_save\', \'jdp_attachment_field_credit_save\', 10, 2 );
?>

1 个回复
最合适的回答,由SO网友:Philip Downer 整理而成

我不完全清楚为什么get\\u posts()函数可以工作,而不是创建WP\\u查询的新实例。根据我对WordPress查询工作原理的理解,get\\u posts()实际上是在创建自己的WP\\u QUERY实例。

不管怎样,这是我目前使用的解决方案。

<?php
    //GET THE PHOTOGRAPHERS
    $args = array(
        \'post_type\' => \'jdp_contributors\',
        \'numberposts\' => -1,
        \'tax_query\' => array(
                    array(
                        \'taxonomy\' => \'jdp_contributor_types\',
                        \'field\' => \'id\',
                        \'terms\' => $terms
                    ),
        )
    );
    $photographers = get_posts($args);

    //LOOP THRU THE PHOTOGRAPHERS
    foreach( $photographers as $post ) {
        $form_fields[\'jdp_photographer\'][\'html\'] .= \'<option value="\'.$post->ID.\'" \'.selected($post->ID, $current_photog, false).\'>\'.$post->post_title.\'</option>\';
    }
?>

相关推荐

get_post_meta pagination

我有自定义字段,具有多个值的数组$metas = get_post_meta($post->ID, \"$meta_key\", false); foreach ( $metas as $metas ){ echo $metas; } 当然,这将返回一个数组。如何对此分页?例如:www.example.com/post-title/1 将返回meta_value[0]和www.example.com/post-title/2 将