How to get all posts in selected taxonimies custom post type? - custom-post-type

I have a post type called 'faq' and taxonomy called 'type' within my template and a few taxonomy terms created "Design", "Print", "Display" etc.
The idea I am trying to implement is to display only the posts that belong to assigned taxonomies (types) without duplication. Each post may be assigned to multiple taxonomies (types).
My current code works fine as long as the post have got only one taxonomy assigned to it. As soon as I assign more then one taxonomy it shows duplicate posts like this:
Question 6
Question 5
Question 1
Question 1
Here is my current code:
<?php
$post_type = 'faq';
$tax = 'type';
$faq_types = get_field('types');
$filtered = array();
$termargs = array( 'include' => $faq_types );
$tax_terms = get_terms($tax, $termargs);
if ($tax_terms) {
$i = 1;
foreach ($tax_terms as $tax_term) {
$args=array(
'post_type' => $post_type,
$tax => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div class="accordion-section">
<a class="accordion-section-title" href="#accordion-<?php echo $i; ?>"><i class="fa fa-chevron-right"></i> <?php the_title(); ?></a>
<div id="accordion-<?php echo $i; ?>" class="accordion-section-content">
<?php the_content(); ?>
</div>
</div>
<?php
$i++;
endwhile;
}
wp_reset_query();
}
}
?>
I'd really appreciate any help with getting this working the way I need.

Your current loop is saying "For each taxonomy term, show all posts associated with that term", so of course it will duplicate if there is one post associated with multiple terms. Take your query out of the foreach loop and use a single tax query with an array of terms:
$args = array(
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $term_slugs,
),
),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
EDIT
By the way, you'll need to convert your array of term objects to an array of term slugs for this to work properly:
$term_slugs = array();
foreach( $tax_terms as $term ) {
$terms_slugs[] = $term->slug;
}

Related

Pagination in search.php not working correctly (Wordpress)

In the past few days I was trying to create a search.php template for results that contain my CPT. As long as the search works itself, pagination not. Every link in pagination redirect to 404. I was trying a lot of possibility solutions from Stack, FB and others but with no results. Instead of showing to you my code wich is obviously not working correctly, I thought how great woultd be when someone could paste here a tested by himself a simple search.php template wchich contain CPT in results and with working pagination? Then I could compare with my code and find a solution for this annoying popular issue :)
Thanks!
edit:
If somoeone is curious, this is my current search.php:
<div class="center">
<?php
// Define custom query parameters
$custom_query_args = array(
'post_type' => array('drzewa_formowane', 'pre_bonsai'),
'post_status' => 'publish',
'order' => 'asc',
's' => $s,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
<?php include 'product.php' ?>
<? endwhile;
endif;
// Reset postdata
wp_reset_postdata(); ?>
<nav class="pagination">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => true,
'prev_text' => __(' « '),
'next_text' => __(' » '),
'type' => 'plain',
) ); ?>
</nav>
<?php
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
I was also trying with simple
if ( have_posts() ) : while ( have_posts() ) : the_post();
without WP_Query and I was trying include CPT to search results via "pre_get_posts" but then was no results from my CPT...

Fuelphp - Using pagination for displaying pages

I'm working on a project with Fuel. I created a pagination , first page has data truly in it but when I click on other pages , url changes but page never refresh and it doesn't show other pages.
codes:
code in Controller :
$config = array(
'pagination_url' => 'http://localhost/body-app/public/app/list/',
'total_items' => 12,
'per_page' => 3,
'uri_segment' => 5,
// or if you prefer pagination by query string
//'uri_segment' => 'page',
);
$pagination = Pagination::forge('mypagination', $config);
$data['example_data'] = DB::select('*')
->from('bodies')
->limit($pagination->per_page)
->offset($pagination->offset)
->execute()
->as_array();
$data['pagination'] = $pagination;
$view=View::forge('app/list',$data);
and my foreach in View is something like this :
<?php foreach($example_data as $per) {
echo $per['name'] ;
} ?>
and finally code to show pagination :
<?php echo Pagination::instance('mypagination')->render(); ?>
If you work with segment number, you should change the route.php file, and add the example 'prod/page/(:page)?' => 'prod' , or add in the you controller one segment string example 'uri_segment' => 'examplenumberpage'.

Display cutsom post type from specific category

I'd like to display custom posts from specific category in Wordpress in custom page.
My code is:
<?php $loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
?>
I'd like to display post from "loze" category.
Greetings
You can use category_name for this. Amend your WP_Query as follows:
$loop = new WP_Query(
array(
'post_type' => 'product',
'category_name' => 'loze',
'posts_per_page' => -1
)
);
However, I would recommend using the category ID instead (referenced using cat instead of category_name) as it's future-proofed somewhat (you could change the name of the category).
There is lots of information on this in the category section of the WordPress Codex for WP_Query.

Attributes of products in cart in opencart

I am rather new to opencart and I'm currently developing a shop where I need to be able to display product attributes inside the cart. I searched allover but apparently no one had the need for this feature.
I short, i tried to duplicate the way attributes are retrieved in the product page but no success.
I modified the controller adding this line:
$this->data['attribute_groups'] = $this->model_catalog_product->getProductAttributes($this->request->get['product_id']);
I also added:
<?php foreach ($attribute_groups as $attribute_group) { ?>
<?php echo $attribute_group['name']; ?>
<?php foreach ($attribute_group['attribute'] as $attribute) { ?>
<?php echo $attribute['name']; ?>
<?php echo $attribute['text']; ?>
<?php } ?>
<?php } ?>
<?php } ?>
with no succes.
I guess I have to modify somehow system/library/cart.php. The problem is that I am not savvy enough to know how to do it!
At this point I decided to ask for help here!
Any ideas please?
This isn't as hard as it would seem on the surface you'll only need to edit 2 files and open 3.
--1-- Add the method for retrieving attributes to the cart class.
Open catalog/model/catalog/product.php
Find the method getProductAttributes($product_id) and copy the entire method to your clipboard.
Open system/library/cart.php and after the getProducts() method paste your copied method.
--2-- Just above where you pasted the code, at the end of the getProducts() method you'll see where the products array is built for the view, it looks similar to this:
$this->data[$key] = array(
'key' => $key,
'product_id' => $product_query->row['product_id'],
'name' => $product_query->row['name'],
'model' => $product_query->row['model'],
'shipping' => $product_query->row['shipping'],
'image' => $product_query->row['image'],
'option' => $option_data,
'download' => $download_data,
'quantity' => $quantity,
'minimum' => $product_query->row['minimum'],
'subtract' => $product_query->row['subtract'],
'stock' => $stock,
'price' => ($price + $option_price),
'total' => ($price + $option_price) * $quantity,
'reward' => $reward * $quantity,
'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $quantity : 0),
'tax_class_id' => $product_query->row['tax_class_id'],
'weight' => ($product_query->row['weight'] + $option_weight) * $quantity,
'weight_class_id' => $product_query->row['weight_class_id'],
'length' => $product_query->row['length'],
'width' => $product_query->row['width'],
'height' => $product_query->row['height'],
'length_class_id' => $product_query->row['length_class_id']
);
Now simply add a call to that getAttributes method to the array:
'attributes' => $this->getProductAttributes($product_query->row['product_id'])
Now open your cart template: catalog/view/theme/yourtheme/common/cart.tpl and right where the product option loop is, you can now loop through your attributes just like the options.

Yii Search in navigation menu

I'm currently writing an application in Yii in which i want to create a search box in the header (either in the navigation menu or just above it). This search box should be able to be accessed from every part of the site, and it should be able to search on different columns of different tables.
I have no idea on how to do this, and almost all posts on the web about it involves using the grid-view, or an extension (I'd like to create the code without an extension if that's possible).
Do you have an idea to how the search code should look (what i should put in which controller etc.)?
-- EDIT --
I still don't know how to do this but I will show you what I have at the moment anyway. It is not much and it is pretty obvious where i am missing some code.
/view/layout/main.php:
<?php echo CHtml::form(Yii::app()->createUrl('product/search'), 'get') ?>
<?php echo CHtml::textField('search_key','',array('placeholder' => 'Search')); ?>
<?php echo CHtml::submitButton('Go'); ?>
<?php echo CHtml::endForm() ?>
/view/product/search.php:
//Not sure by any means what to write here, but I'll like a list view populated with the search results
/controllers/productController.php
/**
* Search through model.
*/
public function actionSearch()
{
if(isset($_GET['search_key'])){
$search = $_GET['search_key'];
$model->name = $search;
}
$this -> render('search', array(
'model' => $model,
));
}
/models/Product.php
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
You can perform like this:
Find #mainmeu in /protected/views/layout/main.php
<div id="mainmenu">
<div style="width: 80%;float: right">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'home', 'url'=>array('/site/index')),
array('label'=>'about', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'contact', 'url'=>array('/site/contact')),
),
)); ?>
</div>
<div style='float: left;direction: rtl; color: #ffffff; margin: 5px 0 0 5px; font-size: 13px'>
<?php echo CHtml::form(Yii::app()->createUrl('product/search'),'get') ?>
<?php echo CHtml::textField('search_key', 'search') ?>
<?php echo CHtml::submitButton(); ?>
<?php echo CHtml::endForm() ?>
</div>
</div><!-- mainmenu -->
Edit:
/models/Product.php:
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('name',$this->name,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
/controllers/productController.php -> actionSearch():
public function actionSearch()
{
$model = new Product('search');
$model->unsetAttributes();
if(isset($_GET['search_key']))
$model->name = $_GET['search_key'];
$this -> render('search', array(
'model' => $model,
));
}
/view/product/search.php:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'product-grid',
'dataProvider'=>$model->search(),
//'filter'=>$model,
'columns'=>array(
'name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>

Resources