Wordpress Loop Tax Query with multiple terms - arrays

I have a taxonomy called "fachbereiche". First I load the taxonomies of the current page:
<?php $term_list = wp_get_post_terms($post->ID, 'fachbereiche', array("fields" => "all", 'parent' => '0'));
foreach($term_list as $thisslug)
{
$output = $thisslug->slug;
echo $output;
?>
The current page has the taxonomy slugs: "bauelemente" and "baumarkt". The echo $output returns bauelementebaumarkt.
Now I want to find all posts of a custom post type "marken" with the same taxonomies as we got above ("bauelemente" and "baumarkt"), so I load the following query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'marken',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=> 'title',
'order'=>'ASC',
'tax_query' => array(
array(
'taxonomy' => 'fachbereiche',
'field' => 'slug',
'terms' => array($output)
),
),
)
);
}
?>
The query returns only the posts with the taxonomy for "baumarkt". I think because the variable $output returns bauelementebaumarkt. I think that you have to seperate "bauelemente" and "baumarkt". Please have in mind that there can be more than 2 terms or just 1.

Your $output should be an array instead of string, so add this before foreach:
$output = array();
Then inside foreach you should do this:
$output[] = $thisslug->slug;
And finally in tax_query it should be like this:
'terms' => $output,

Related

How can I get an array of custom categories in Wordpress?

I have registered a custom taxonomy as part of my custom post type, but when passing it through to get_categories() it returns an empty array. Any ideas as to why?
// Register FAQ Categories taxonomy
function bv_faq_register_categories() {
register_taxonomy(
'faq-category',
'faq',
array(
'label' => 'Categories',
'rewrite' => array('slug' => 'faq-category'),
'hierarchical' => true
)
);
}
add_action('init', 'bv_faq_register_categories');
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category'
));
$categories is returning an empty array.
Have you tried get_terms instead?
$categories = get_terms( 'faq-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
Like #AD Styles said I would use get_terms using the custom taxonomy, to expand a bit here's some example code:
<?php
$post_type = 'faq-category';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'parent' => 0
) );
foreach( $terms as $term ) :
echo "<h1>".$term->name."</h1>";
endforeach;
endforeach;
?>
Your code looks ok. Do you have assigned this category to any post/post type?
If not then you will get an empty result. for testing you can set 'hide_empty' = false like this:
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category',
'hide_empty' => false // set it true
));
Also, you can use get_terms() function.

how to fetch data from database in drupal7 in table form

how to fetch data from database in drupal7:
fields i have name:
subject:
email:
message:
give me the code for drupal 7 i want it in table form.
my insert code is this:
function form_example_form_submit($form, &$form_state) {
echo $name = $form_state['values']['textfield'];
echo $email = $form_state['values']['mail'];
echo $subject = $form_state['values']['subject'];
echo $message = $form_state['values']['message'];
echo $ip=ip_address();
echo $cb=$name;
//echo $timestamp = REQUEST_TIME;
echo $time=time();
$nid=db_insert('form') // Table name no longer needs {}
->fields(array(
'name' => $name,
'email' => $email,
'subject' => $subject,
'message' => $message,
'ip' => $ip,
'created_by' => $cb,
//'created_at' => $time,
))
->execute();
//print_r($nid);
drupal_set_message(t('The form has been submitted.'));
}
how can i fetch the data from database in drupal 7 in the form of table ,
give me the code .i m newbie in drupal 7 so its very difficult forme
Here is the snippet to fetch all content from "form" table with table format and pager. Also you can add condition which I have added in comment if required.
<?php
// Set header
$header = array(
array('data' => t('Name'), 'field' => 'name'),
array('data' => t('Email'), 'field' => 'email'),
array('data' => t('Subject'), 'field' => 'subject'),
array('data' => t('Message'), 'field' => 'message'),
);
//query to fetch all content
$query = db_select('form', 'f');
$query->fields('f');
//$query->condition('f.name', $search_name, '=') //if needed
$table_sort = $query->extend('TableSort') // Table sort extender
->orderByHeader($header); // Order by headers
$pager = $table_sort->extend('PagerDefault')
->limit(20); // Set page limit
$arr_result = $pager->execute();
$rows = array();
foreach($arr_result as $result) {
$rows[] = array(
$result->name,
$result->email,
$result->subject,
$result->message,
);
}
// Set empty output
$output = '';
if (!empty($rows)) {
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
/*'attributes' => array(
'id' => 'sort-table' // add if want to add sorting
) */
));
$output .= theme('pager');
}
else {
$output .= t("No results found.");
}
return $output;
?>
Let me know if any query/confusion occurs for the same.

Loop through 4 post types and only return 1 post per type

I'm trying to understand the most efficient way to return 4 most recent posts. 1 from each post type. There are 4 post types.
I could do several things such as 4 different queries. Or count each post within each post type returned, then only allow the first to display. But everything I'm coming up with seems really overcomplicated.
Here is what I have so far, returning 4 posts of whatever post types are recent
<?php
$newsArgs = array(
'posts_per_page' => 4,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => array('post', 'news', 'press', 'casestudy'),
'post_status' => 'publish',
'suppress_filters' => true,
);
$query = new WP_Query( $newsArgs );
if ( $query -> have_posts()) {
while ( $query -> have_posts() ) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile;
}
wp_reset_postdata();
?>
</ul>
Without creating a custom query to handle this (which in itself is going to be VERY ugly to look at), Wordpress doesn't have much in the way of supporting that without at least than 4 or 5 separate queries per request.... very expensive
To ensure you get at least one of each post type without too much care about performance, you can do something like this:
$results = array();
foreach(array('post', 'news', 'press', 'casestudy') as $type){
$post = get_posts(array(
'posts_per_page' => 1,
'post_type' => $type,
'post_status' => 'publish'
));
$results[] = $post[0]->ID;
}
$query = new WP_Query(array(
'orderby' => 'post_date',
'order' => 'DESC',
'suppress_filters' => true,
'post__in' => $results
));
if ( $query -> have_posts()) {
while ( $query -> have_posts() ) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile;
}
wp_reset_postdata();
It's untested, but it should help get you to where you need.

Cakephp foreach data for options in select (form)

in my project I have database
"Products" hasAndBelongsToMany "Sizes"
"ProductsSize" belongsTo "Products"
"ProductSize" belongsTo "Size"
In ProductsSize, one product_id may have many size_id.
hence, i want to do a form that have a select type input that list down all the size_id where the product_id = $id.
What i have done is:
in controller:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
in view:
<?php echo $this->form->create('Cart', array('action' => 'add')); ?>
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
then i got error: Undefined index: ProductsSize
but when i put foreach, the data shown:
<?php foreach ($size_apparel as $size): ?>
<?php echo $size['ProductsSize']['size_id'];?><br/>
<?php endforeach; ?>
can anyone please help me to do the foreach in options.
The problem is that you are using an invalid array for the options. You are requesting:
$size = $this->Product->ProductsSize->find('all', array(
'conditions' => array('ProductsSize.product_id' => $id),
'fields' => array('ProductsSize.size_id'),
));
What you should be requesting is:
$size = $this->Product->ProductsSize->find('list', array(
'conditions' => array('ProductsSize.product_id' => $id),
));
This will return the options for the form field in the format it expects which is:
array(
{id} => {size},
{id} => {size},
{id} => {size},
{id} => {size},
...
)
I think you meant to say
<?php echo $this->form->input('size_id', array('label' => 'Size', 'options' => $size)); ?>
note 'options' instead of 'value'
$List = Set::classicExtract($size, '{n}.ProductSize.product_id');
$List will contain key value(ProductSize.product_id) pair.
OR you can change your query as
$size = $this->Product->ProductSize->find('list', array(
'conditions' => array('ProductSize.product_id' => $id),
));
Hope it will work for you

Paginate a search result in CakePHP

I've set a simple search engine on my CakePHP project which looks like that :
<?php
echo $this->Form->create("Post", array(
"action" => "search",
"id" => "searchForm"
));
echo $this->Form->input("keyword", array(
"label" => "",
"type" => "search",
"placeholder" => "Recherche..."
));
echo $this->Form->end();
?>
Here is the controller :
function search() {
$keyword = $this->request->data;
$keyword = $keyword["Post"]["keyword"];
$cond = array("OR" => array(
"Post.title LIKE '%$keyword%'",
"Post.description LIKE '%$keyword%'"
));
$posts = $this->Post->find("all", array("conditions" => $cond));
$this->set(compact("posts", "keyword"));
}
And it works great. The only problem is when I want to paginate the results. I simply add :
$posts = $this->paginate();
And here is the problem. When I add this, CakePHP give me all the posts and not only the ones that match the keyword.
So, if you would have a solution, it would be nice :)
According to the CakePHP book you should be able to do
$this->paginate('Post', array(
'OR' => array(
'Post.title LIKE' => "%$keyword%",
'Post.description LIKE' => "%$keyword%"
)
));
Or you can do it like this ( from the cakephp site ).
public function list_recipes() {
$this->paginate = array(
'conditions' => array('Recipe.title LIKE' => 'a%'),
'limit' => 10
);
$data = $this->paginate('Recipe');
$this->set(compact('data'));
);
Source:
http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
Paginate does its own lookup on the data I believe. The find you are calling previously has no effect on the paginate.
Try this:
$this->paginate = array( 'conditions' => $cond, ));
you can use Session for storing conditions
the first, when you submit form you store conditions into Session
and then(paginate) you can read conditions from Session
example i want to search the products:
Products/search.ctp
<?php
echo $this->Form->create('Product');
echo $this->Form->input('keyword');
echo $this->Form->end(__('Search'));
?>
ProductsController.php
<?php
class ProductsController extends AppController{
public function search() {
if ($this->request->is('post')) {
$keyword = $this->request->data['Product']['keyword'];
$this->paginate = array(
'fields' => array('Product.name', 'Product.price', 'Product.created'),
'order' => array('Product.created' => 'DESC', 'Product.price' => 'DESC'),
'limit' => 30,
'conditions' => array('Product.name LIKE' => '%' . $keyword . '%')
);
// store array $this->paginate into Session
$this->Session->write('paginate', $this->paginate);
}
$this->paginate = $this->Session->read('paginate');
$this->set('products', $this->paginate('Product'));
}
}

Resources