Wordpress custom fields - nesting foreach - wordpress

I'm a wordpress beginner.
I have some custom fields that contain multiple values, and I want to output them.
One custom field has multiple values (press review quotes for a show - reviewchart-review). But I also want it to output the corresponding value in the reviewchart-publication field (the name of the publication the press quote is from).
This is the code:
$post_meta_array = get_post_meta($post->ID, 'reviewchart-review');
$review_pub = get_post_meta($post->ID, 'reviewchart-publication');
foreach ($post_meta_array as $post_meta) {
echo $review_pub . '<blockquote><p>' . $post_meta . '</p></blockquote>';
}
This is the output:
Array
"Fresh and assured. This Restoration comedy has japes aplenty. Winning performances from Susannah Fielding and Geoffrey Streatfeild."
Array
“Simon Godwin’s production of George Farquhar’s play is one of the most consistently funny productions of a Restoration comedy you’re likely to see.”
Any help appreciated,
Thanks

Related

Fetch a post under certain no of words or characters in wordpress?

Is it possible to fetch a post with content under 140 characters or 25 words ?
if possible how to do it
here is my random post code
// Random post link
function randomPostlink(){
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) : $RandPostQuery->the_post();
echo the_permalink();
endwhile;
wp_reset_postdata();
}
Character count is easy, you can just add the condition AND CHAR_LENGTH(post_content) < 140 to your where clause.
Word count is more difficult because there is no built in MySQL function for counting words. You can find simple solutions that don't work in every use case as well as complete solutions that use stored functions. I'll use a simple solution for the sake of example.
What you need to do is add a filter to the where clause and apply your additional conditions there:
add_filter( 'posts_where', 'venki_post_length_limit' );
function venki_post_length_limit($where = '') {
remove_filter( 'posts_where', 'venki_post_length_limit' );
$where .= ' AND (
CHAR_LENGTH(post_content) < 140 OR
(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) < 25
) ';
return $where;
}
Notice that I remove the filter as soon as the function is called. This is so you don't apply this same condition to every query.
You should also be aware that both of those conditions are costly compared to a simple lookup on a column value (especially the word count). Neither can utilize indexes. If you have a large number of posts you may run into performance issues if you're running this query frequently. A better solution might be to calculate the word and character count when the post is created/updated and store that as meta data.

Query Posts issue in wordpress

I am a little unsure how to do a WP_Query to search a meta value of my posts. The meta data includes a persons height. So valid entries would be 5'9" or 4'11" and I need to do a Compare-Between.
To make it a bit clearer: I have a filter page. So the user can select the height between 4'0" - 5'9 or 5'9" - 5'11"
The problem is I have the ' and " symbols. I can remove these. But then it will be searching between 59 and 511 which is not going to work.
Anyone know of a work around?
Save the values in inches.
You could then set up a WP_Query or a $wpdb query to fetch your results.
Whenever you are displaying the values on the front end, you convert them back to feet and inches.
update:
function convert_to_feet( $input){
$feet = (int) ($input/12);
$inches = $input%12;
return $feet . "'" . $inches . '"';
}
echo convert_to_feet(71);

Drupal Views arguments should show both synonyms and siblings

I am working with Drupal Views, that filter our content based on geography, and shows news from a specific municipality.
Our taxonomy is hierarchical:
Regions
-- Municipalities from the region
--- Towns from the municipality
For example:
Region A (region)
-- Municipality X (municipality)
--- Town 1
--- Town 2
-- Municipality Y (municipality)
--- Town 3
--- Town 4
-- Municipality Z (municipality)
--- Town 5
--- Town 6
We tag our nodes with town names, and by using taxonomy term id as argument in our view, we can easily list all stories from any municipality.
Now we would like to add a new feature to our view: Also list news from the neighbouring municipality. Municiplaity Y is a Neghbour of Municipality Z, and we added this relation to the taxonomy term.
So now we can show all stories from Y by choosing using Taxonomy Depth argument. We can also show all stories, by using the Taxonomy Related terms argument.
But how to show all nodes from both Y and its sibling Z with views?
You should change the validator of the views argument to PHP code. Then, in the validator field you will be able to change the argument dynamically. This way, having the tid you can get all the related terms.
set the Action to take if argument is not present to "display empty text" ;
on validator options, set the validator to "PHP Code";
in the code:
get the tid using arg(1);
use taxonomy_get_related($tid) to get the related terms;
build a string in with the syntax "tid+related_tid+related_tid+related_tid" to return as argument;
redefine $handler->argument as the built string;
finally, set the Allow multiple terms per argument as True
Here's an example code to insert in the php code validation form:
$tid = arg(1);
$result = strval($tid);
$related = taxonomy_get_related($tid);
foreach($related as $i){
if (intval($i)>0){
$result.="+".$i;
}
}
$handler->argument = $result;
return $result;

Get next,previous post in 2 level categories in wordpress

I have 2 level categories like this:
paintings
- car
- cat
- dog
other
- other1
- other2
...
Now I've many posts in each sub-category.
My goal is get the next post and previous post from the current post in current main category.
For example : I've a post name : "a good car" in car category. Then I've a post name : "A big cat " in cat category . Then I've a post name : "a small dog " in dog category.
Now in the "a big cat" post , I'm trying to get the 'a small dog' post as next post and the 'a good car' as prev post.
I'm used :
codex.wordpress.org/Function_Reference/get_next_post
and :
codex.wordpress.org/Function_Reference/get_previous_post
but they still can't get the right post for me.
If I try with :
get_previous_post( false, '' )
I will get the previous post but not in painting category.
Also If I try with:
get_previous_post( true, '' )
I will get only the previous post in the current category , not the painting category.
Anyone can help me ? Thank you!
First, I would be sure that your "cat" paintings belong to both the "cat" and "paintings" categories. By default, I believe, the WordPress PREV/NEXT functions will work the way you're explaining.
i would expect this to work for you, provided your items are included in both the sub and main categories:
<?php previous_post_link('%link', 'Previous in category', TRUE); ?>
More details:
http://codex.wordpress.org/Template_Tags/previous_post_link and
http://codex.wordpress.org/Template_Tags/next_post_link

Filter a product collection by two categories in Magento

I'm trying to find products that are in two categories.
I've found an example to get products that are in category1 OR category2.
http://www.alphadigital.cl/blog/lang/en-us/magento-filter-by-multiple-categories.html
I need products that are in category1 AND category2.
The example in the blog is:
class ModuleName_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection{
public function addCategoriesFilter($categories){
$alias = 'cat_index';
$categoryCondition = $this->getConnection()->quoteInto(
$alias.'.product_id=e.entity_id AND '.$alias.'.store_id=? AND ',
$this->getStoreId()
);
$categoryCondition.= $alias.'.category_id IN ('.$categories.')';
$this->getSelect()->joinInner(
array($alias => $this->getTable('catalog/category_product_index')),
$categoryCondition,
array('position'=>'position')
);
$this->_categoryIndexJoined = true;
$this->_joinFields['position'] = array('table'=>$alias, 'field'=>'position' );
return $this;
}
}
When I'm using this filter alone it perform OR query on several categories.
When I combine this filter with prepareProductCollection of Mage_Catalog_Model_Layer
it somehow remove the filter effect.
How can I change the filter to AND and combine it with prepareProductCollection?
Thanks
Thanks
This code will allow you to filter by multiple categories but avoid completely killing performance if you had to perform multiple collection loads:
$iNumberFeaturedItems = 4;
$oCurrentCategory = Mage::registry('current_category');
$oFeaturedCategory = Mage::getModel('catalog/category')->getCollection()
->addAttributeToFilter('name','Featured')
->getFirstItem();
$aFeaturedCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'url_key'), 'inner')
->addStoreFilter()
->addCategoryFilter($oFeaturedCategory)
->addCategoryIds();
The first step is to get a collection of products for one category (in this case, a Featured category). Next step is to get the IDs of the products, notice that this does NOT perform a load (ref Mage_Core_Model_Mysql4_Collection_Abstract::getAllIds())
$aFeaturedProdIds = $aFeaturedCollection->getAllIds();
shuffle($aFeaturedProdIds); //randomize the order of the featured products
Then get the IDs for a second category:
$aCurrentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
And intersect the arrays to find product IDs that exist in both categories:
$aMergedProdIds = array_intersect($aFeaturedProdIds,$aCurrentCatProdIds);
For this particular use case, we loop until we have sufficient intersecting products, traversing up the category tree until we find a large enough match (but stopping at root category!):
while(count($aMergedProdIds) < $iNumberFeaturedItems && $oCurrentCategory->getId() != Mage::app()->getStore()->getRootCategoryId()):
$oCurrentCategory = $oCurrentCategory->getParentCategory();
$aParentCatProdIds = $oCurrentCategory->getProductCollection()->getAllIds();
$aMergedProdIds = array_intersect($aFeaturedProdIds,$aParentCatProdIds);
endwhile;
Finally, filter our initial collection by the IDs of the intersecting products, and return.
$aFeaturedItems = $aFeaturedCollection->addIdFilter(array_slice($aMergedProdIds,0,$iNumberFeaturedItems))->getItems();
return $aFeaturedItems;
I am also working on this to no avail, it was available in magento 1.3 using the attribute filter with finset on the category_ids column, however this was moved into the index table from the entity table and now no longer works.
There is one possible solution, but requries an override function which I found here
But this solution is far from ideal.
I think that it will be enough to call addCategoryFilter() twice on your product collection - once for each category. I have not tested it though, so might be wrong.

Resources