PHP code example of tacowordpress2 / addmany

1. Go to this page and download the library: Download tacowordpress2/addmany library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tacowordpress2 / addmany example snippets



// Add this so your project has access to Composer's autoloaded files.
// Please replace "{path_to_autolaod}".
ze AddMany
\Taco\AddMany\Loader::init();



// Example configuration for a basic AddMany Field

  public function getFields() {
    return [
      'staff_members' => \Taco\AddMany\Factory::create(
        [
          'first_name' => ['type' => 'text'],
          'last_name' => ['type' => 'text'],
          'bio' => ['type' => 'textarea']
        ],
        ['limit_range' => [2, 3]] // Enforce a minimum of 2 items, but no more than 3.
       )->toArray()
    ];
  }

// Example configuration for an AddMany field with AddBySearch
// Adds a search field for querying posts via AJAX

  public function getFields() {
    return [
      'employees' => \Taco\AddMany\Factory::createWithAddBySearch('Employee')->toArray()
    ];
  }
 

// Example AddBySearch with shared fields

class Store extends \Taco\Post {
  public function getFields() {
    return [
      'products' => \Taco\AddMany\Factory::createWithAddBySearch('Product',[
        'price' => ['type' => 'text'],
        'tax' => ['type' => 'text']
      ])->toArray()
    ];
  }
 }
 


// Example AddMany field with field variations – Adds a dropdown for users to select

  public function getFields() {
    return [
      'staff_members' => \Taco\AddMany\Factory::create(
        [
          'board_members' => [
            'first_name' => ['type' => 'text'],
            'last_name' => ['type' => 'text'],
            'bio' => ['type' => 'textarea']
          ],
          'general_staff' => [
            'first_name' => ['type' => 'text'],
            'last_name' => ['type' => 'text'],
            'department' => ['type' => 'select', 'options' => $this->getDepartments()]
          ],
        ]
       )->toArray()
    ];
  }


// You can simulate a one-to-one relationship by limiting the number of items to 1

class Person extends \Taco\Post {
  public function getFields() {
    return [
      'spouse' => \Taco\AddMany\Factory::create(
        [
          'first_name' => ['type' => 'text'],
          'phone' => ['type' => 'text']
        ],
        ['limit_range' => [0, 1]] // Do not allow more than 1 item to be added
       )->toArray()
    ];
  }
 }


class Post extends \Taco\Post {
  use \Taco\AddMany\Mixins;
  ...

// In your template (Example)

$blog_post = \Taco\Post\Factory::create($post); 

  public function getFallBackRelatedPosts($key) {
    global $post;
    $post_id = (is_object($post) && isset($post->ID))
      ? $post->ID
      : null;
    if($key === 'related_posts') {
      return \Taco\Post::getWhere(['posts_per_page' => 3, 'exclude' => $post_id]);
      // The above actually just gets the 3 most recent posts, excluding the current one.
      // This is a poor example. Don't be this lazy!
    }
  }

public function getFields() {
  return [
    'videos' => \Taco\AddMany\Factory::createAndGetWithAddBySearch('GalleryItem', null, [
    'uses_ordering' => true
  ]);
];


class Store extends \Taco\Post {
  use \Taco\AddMany\Mixins;

  public function getFields() {
    return [
      'products' => \Taco\AddMany\Factory::createWithAddBySearch('Product', [
        'price' => ['type' => 'text']
      ])->toArray()
    ];
  }
  ...

/* Single Store - single-store.php */
 $store = Store::find($post->ID); // load the store and iterate through the products

foreach($store->products as $product): 

  public static function getPairsWithKeyWords($keywords, $post_type_class_name, $custom_args=[]) {
    $post_type = Str::machine(Str::camelToHuman($post_type_class_name), '-');

    $query = new \WP_Query([
      'post_type' => $post_type,
      's' => $keywords,
      'posts_per_page' => -1
    ]);

    $results = [];
    foreach ($query->posts as $post) {
      $results[$post->ID] = $post->post_title;
    }

    return $results;
  }

 public static function getPeopleByPersonTypeTerm($keywords, $post_type_class_name, $custom_args=['terms' => 'employee') {
      $post_type = Str::machine(Str::camelToHuman($post_type_class_name), '-');

      $query = new \WP_Query([
          'post_type' => $post_type,
          's' => $keywords,
          'tax_query' => [
              [
                  'taxonomy' => 'person-type',
                  'field'    => 'slug',
                  'terms'    => $custom_args['terms']
              ],
          ),
          'posts_per_page' => -1
      ]);

      $results = [];
      foreach ($query->posts as $post) {
          $results[$post->ID] = $post->post_title;
      }
      return $results;
  }

public function getFields() {
  return [
    'products' => \Taco\AddMany\Factory::createWithAddBySearch('Person::getPeopleByPersonTypeTerm', null, [ 'args' => ['terms' => 'employee'] ])
  ];
}


public function getFields() {
  return [
    'right_column_images' => \Taco\AddMany\Factory::create([
      'image' => ['type' => 'image']
    ], ['show_on_collapsed' => 'image'])->toArray()
  ];
)'