本文整理汇总了PHP中wp_set_post_categories函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_set_post_categories函数的具体用法?PHP wp_set_post_categories怎么用?PHP wp_set_post_categories使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_set_post_categories函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _rs_mt_set_categories
function _rs_mt_set_categories($args)
{
global $wp_xmlrpc_server;
$wp_xmlrpc_server->escape($args);
$post_ID = (int) $args[0];
$username = $args[1];
$password = $args[2];
$categories = $args[3];
if (!($user = $wp_xmlrpc_server->login($username, $password))) {
return $wp_xmlrpc_server->error;
}
if (empty($categories)) {
$categories = array();
}
$catids = array();
foreach ($categories as $cat) {
$catids[] = $cat['categoryId'];
}
$catids = apply_filters('pre_object_terms_rs', $catids, 'category');
do_action('xmlrpc_call', 'mt.setPostCategories');
if (!get_post($post_ID)) {
return new IXR_Error(404, __('Invalid post ID.'));
}
if (!current_user_can('edit_post', $post_ID)) {
return new IXR_Error(401, __('Sorry, you cannot edit this post.'));
}
wp_set_post_categories($post_ID, $catids);
return true;
}
示例2: save_default_post_categories
/**
* This function set post in default category.
*
* @param $post_ID
*
* @return mixed
*/
public function save_default_post_categories($post_ID)
{
if (wp_is_post_autosave($post_ID) || wp_is_post_revision($post_ID)) {
return $post_ID;
}
$categories = wp_get_post_categories($post_ID);
if (count($categories) === 1 && $categories[0] === (int) get_option('default_category')) {
$cats = (array) $this->get_default_post_category_by_lang(get_post_meta($post_ID, 'translation_lang', true));
wp_set_post_categories($post_ID, $cats);
}
}
开发者ID:albertso,项目名称:Unyson-Translation-Extension,代码行数:18,代码来源:class-fw-extension-translate-terms.php
示例3: testSetupMulticomicPartialPaths
function testSetupMulticomicPartialPaths()
{
$cp = $this->getMock('ComicPress', array('_is_dir'));
wp_set_post_categories(1, array('2', '3'));
add_category('2', (object) array('slug' => 'test-one'));
add_category('3', (object) array('slug' => 'test-two'));
$cp->expects($this->at(0))->method('_is_dir')->with('/subthemes/test-one')->will($this->returnValue(true));
$cp->expects($this->at(1))->method('_is_dir')->with('/subthemes/test-two')->will($this->returnValue(false));
$cp->setup_multicomic_partial_paths(1);
$this->assertEquals(array('/subthemes/test-one'), $cp->partial_paths);
}
示例4: test_dotted_formats
/**
* @ticket 21167
*/
public function test_dotted_formats()
{
global $wp_rewrite;
// Create a sample post
$cat_id = $this->factory->term->create(array('name' => 'permalink-test', 'taxonomy' => 'category'));
$user_id = $this->factory->user->create(array('role' => 'editor', 'user_login' => 'permalink_user'));
$post_id = $this->factory->post->create(array('post_title' => 'lorem-ipsum', 'post_date' => '2012-08-02 14:15:05', 'post_author' => $user_id, 'category' => $cat_id));
wp_set_post_categories($post_id, array($cat_id));
// Sample permalinks
$tests = array('/%postname%.%post_id%/ ' => array('regex' => '([^/]+)\\.([0-9]+)(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&p=$2&page=$3'), '/%year%.%monthnum%.%postname%/' => array('regex' => '([0-9]{4})\\.([0-9]{1,2})\\.([^/]+)(/[0-9]+)?/?$', 'url' => 'index.php?year=$1&monthnum=$2&name=$3&page=$4'), '/%post_id%.%postname%/' => array('regex' => '([0-9]+)\\.([^/]+)(/[0-9]+)?/?$', 'url' => 'index.php?p=$1&name=$2&page=$3'), '/%postname%.%year%/' => array('regex' => '([^/]+)\\.([0-9]{4})(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&year=$2&page=$3'), '/$%postname%$/' => array('regex' => '\\$([^/]+)\\$(/[0-9]+)?/?$', 'url' => 'index.php?name=$1&page=$2'), '%year%.+?%monthnum%.+?%day%.+?%hour%.+?%minute%.+?%second%.+?%post_id%.+?%postname%.+?%category%.+?%author%.+?' => array('regex' => '([0-9]{4})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]{1,2})\\.\\+\\?([0-9]+)\\.\\+\\?([^/]+)\\.\\+\\?%category%\\.\\+\\?([^/]+)\\.\\+\\?(/[0-9]+)?/?$', 'url' => 'index.php?year=$1&monthnum=$2&day=$3&hour=$4&minute=$5&second=$6&p=$7&name=$8&%category%$9&author_name=$10&page=$11'));
// Test permalinks
foreach ($tests as $permalink_format => $expected) {
update_option('permalink_structure', $permalink_format);
// Get the rewrite rules
$rules = $wp_rewrite->generate_rewrite_rules(get_option('permalink_structure'), EP_PERMALINK, false, false, false, false);
// Filter out only the post rewrite rule
foreach ($rules as $regex => $url) {
if (false === strpos($url, 'attachment=$') && false === strpos($url, 'tb=') && false === strpos($url, 'cpage=$')) {
break;
}
}
// Test that expected === actual
$this->assertEquals($regex, $expected['regex'], "Problem with permalink format: {$permalink_format}");
$this->assertEquals($url, $expected['url'], "Problem with permalink format: {$permalink_format}");
}
}
示例5: test_category__and_var
function test_category__and_var() {
$term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) );
$term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) );
$post_id = $this->factory->post->create();
wp_set_post_categories( $post_id, $term_id );
$posts = $this->q->query( array( 'category__and' => array( $term_id ) ) );
$this->assertEmpty( $this->q->get( 'category__and' ) );
$this->assertCount( 0, $this->q->get( 'category__and' ) );
$this->assertNotEmpty( $this->q->get( 'category__in' ) );
$this->assertCount( 1, $this->q->get( 'category__in' ) );
$this->assertNotEmpty( $posts );
$this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) );
$posts2 = $this->q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) );
$this->assertNotEmpty( $this->q->get( 'category__and' ) );
$this->assertCount( 2, $this->q->get( 'category__and' ) );
$this->assertEmpty( $this->q->get( 'category__in' ) );
$this->assertCount( 0, $this->q->get( 'category__in' ) );
$this->assertEmpty( $posts2 );
}
示例6: testGetValidPostCategory
/**
* @dataProvider providerTestGetValidPostCategory
*/
function testGetValidPostCategory($post_categories, $expected_result)
{
$css = $this->getMock('ComicPressStoryline', array('valid'));
$css->expects($this->any())->method('valid')->will($this->returnValue(true));
wp_set_post_categories(1, $post_categories);
$this->assertEquals($expected_result, $css->get_valid_post_category(1));
}
示例7: set_post_categories
/**
* Update the post categories based on all post category fields
*
* @since 1.17
*
* @param array $form Gravity Forms form array
* @param int $entry_id Numeric ID of the entry that was updated
*
* @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. false if there are no post category fields or connected post.
*/
public function set_post_categories($form = array(), $entry_id = 0)
{
$entry = GFAPI::get_entry($entry_id);
$post_id = rgar($entry, 'post_id');
if (empty($post_id)) {
return false;
}
$return = false;
$post_category_fields = GFAPI::get_fields_by_type($form, 'post_category');
if ($post_category_fields) {
$updated_categories = array();
foreach ($post_category_fields as $field) {
// Get the value of the field, including $_POSTed value
$field_cats = RGFormsModel::get_field_value($field);
$field_cats = is_array($field_cats) ? array_values($field_cats) : (array) $field_cats;
$field_cats = gv_map_deep($field_cats, 'intval');
$updated_categories = array_merge($updated_categories, array_values($field_cats));
}
// Remove `0` values from intval()
$updated_categories = array_filter($updated_categories);
/**
* @filter `gravityview/edit_entry/post_categories/append` Should post categories be added to or replaced?
* @since 1.17
* @param bool $append If `true`, don't delete existing categories, just add on. If `false`, replace the categories with the submitted categories. Default: `false`
*/
$append = apply_filters('gravityview/edit_entry/post_categories/append', false);
$return = wp_set_post_categories($post_id, $updated_categories, $append);
}
return $return;
}
示例8: ajax_new
function ajax_new()
{
check_ajax_referer('fee-new', 'nonce');
require_once ABSPATH . '/wp-admin/includes/post.php';
$post = get_default_post_to_edit(isset($_POST['post_type']) ? $_POST['post_type'] : 'post', true);
wp_set_post_categories($post->ID, array(get_option('default_category')));
wp_send_json_success(get_permalink($post->ID));
}
示例9: testFindParents
/**
* @dataProvider providerTestFindParents
*/
function testFindParents($post_categories, $expected_result)
{
add_category(1, (object) array('slug' => 'root', 'parent' => 0));
add_category(2, (object) array('slug' => 'comic', 'parent' => 1));
add_category(3, (object) array('slug' => 'part-1', 'parent' => 2));
add_category(4, (object) array('slug' => 'blog', 'parent' => 1));
wp_set_post_categories(1, $post_categories);
$this->p->post = (object) array('ID' => 1);
$this->assertEquals($expected_result, $this->p->find_parents());
}
示例10: ahny_update_post_terms
function ahny_update_post_terms($post_id)
{
$post = get_post($post_id);
if ('post' === $post->post_type) {
$categories = wp_get_post_categories($post_id);
$newcat = get_term_by('name', 'Living Modern', 'category');
array_push($categories, $newcat->term_id);
wp_set_post_categories($post_id, $categories);
}
}
示例11: wpSetUpBeforeClass
public static function wpSetUpBeforeClass($factory)
{
self::$posts[0] = $factory->post->create(array('post_name' => 'post0'));
self::$posts[1] = $factory->post->create(array('post_name' => 'post1'));
self::$cats[0] = $factory->category->create(array('slug' => 'cat0'));
self::$cats[1] = $factory->category->create(array('slug' => 'cat1'));
self::$cats[2] = $factory->category->create(array('slug' => 'cat2'));
wp_set_post_categories(self::$posts[0], self::$cats[2]);
wp_set_post_categories(self::$posts[0], self::$cats[0]);
wp_set_post_categories(self::$posts[1], self::$cats[1]);
}
示例12: test_category_name_change
/**
* @ticket 22526
*/
function test_category_name_change()
{
$term = self::factory()->category->create_and_get(array('name' => 'Foo'));
$post_id = self::factory()->post->create();
wp_set_post_categories($post_id, $term->term_id);
$post = get_post($post_id);
$cats1 = get_the_category($post->ID);
$this->assertEquals($term->name, reset($cats1)->name);
wp_update_term($term->term_id, 'category', array('name' => 'Bar'));
$cats2 = get_the_category($post->ID);
$this->assertNotEquals($term->name, reset($cats2)->name);
}
示例13: test_should_not_filter_out_duplicate_terms_associated_with_different_objects
/**
* @ticket 11003
*/
public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects()
{
$post_id1 = self::factory()->post->create();
$post_id2 = self::factory()->post->create();
$cat_id = self::factory()->category->create();
$cat_id2 = self::factory()->category->create();
wp_set_post_categories($post_id1, array($cat_id, $cat_id2));
wp_set_post_categories($post_id2, $cat_id);
$terms = wp_get_object_terms(array($post_id1, $post_id2), 'category');
$this->assertCount(2, $terms);
$this->assertEquals(array($cat_id, $cat_id2), wp_list_pluck($terms, 'term_id'));
$terms2 = wp_get_object_terms(array($post_id1, $post_id2), 'category', array('fields' => 'all_with_object_id'));
$this->assertCount(3, $terms2);
$this->assertEquals(array($cat_id, $cat_id, $cat_id2), wp_list_pluck($terms2, 'term_id'));
}
示例14: testBuildPostTable
function testBuildPostTable()
{
$this->markTestIncomplete();
$posts = array((object) array('ID' => 1, 'post_date' => '2009-01-01', 'post_title' => 'Post 1', 'guid' => 'post-1'), (object) array('ID' => 2, 'post_date' => '2009-01-02', 'post_title' => 'Post 2', 'guid' => 'post-2'), (object) array('ID' => 3, 'post_date' => '2009-01-03', 'post_title' => 'Post 3', 'guid' => 'post-3'));
foreach ($posts as $post) {
wp_insert_post($post);
}
$categories = array(1 => array(1), 2 => array(1), 3 => array(2));
foreach ($categories as $id => $cats) {
wp_set_post_categories($id, $cats);
}
$output = '<div class="related_posts"><span>Title</span><table class="month-table"><tr><td class="archive-date" align="right">Jan 3, 2009</td><td class="archive-title"><a title="Post 3" href="post-3">Post 3</a></td></tr></table></div>';
$this->rp->related_categories = array(2);
$this->assertEquals($output, $this->rp->build_post_table('Title', $posts));
}
示例15: wp_create_categories
/**
* Create categories for the given post.
*
* @since 2.0.0
*
* @param array $categories List of categories to create.
* @param int $post_id Optional. The post ID. Default empty.
* @return List of categories to create for the given post.
*/
function wp_create_categories($categories, $post_id = '')
{
$cat_ids = array();
foreach ($categories as $category) {
if ($id = category_exists($category)) {
$cat_ids[] = $id;
} elseif ($id = wp_create_category($category)) {
$cat_ids[] = $id;
}
}
if ($post_id) {
wp_set_post_categories($post_id, $cat_ids);
}
return $cat_ids;
}