mai 182012
 

Recently I’ve had to group the search results of a website by its post type, and I couldn’t use Relevanssi due to infrastructure reasons. This was a major problem to me due to the lack of documentation or tutorials over the internet.

The solution is quite simple, tho:

On functions.php insert the following code lines:

	if( !is_admin() && isset($_GET['s']) && $_GET['s'])
	{
		add_filter( 'posts_orderby', 'separate_result_types', 1 );
	}

	function separate_result_types($query)
	{
		global $wpdb;
		$query = "$wpdb->posts.post_type, $wpdb->posts.post_date  DESC";
		return $query;
	}

Once wordpress doesn’t support a post_type orderby on the WP_Query queryvars, you have to add a hook that will be activated when a query is about to run. This will order the posts for its post_type, and then for the post_date (which is the default ordination for wordpress) so you won’t lose the order on other loops.

Then, on your search.php (or whatever loop file you’re using), use the loop like this one:

	<?php if(have_posts()): ?>
		<?php
			//This will store the last post type in a var, and every time it changes, it'll print the post type separator.
			$lastType = false;
			$counter = 0;
			while (have_posts()): the_post();
				if ($lastType != $post->post_type):
					$counter++;
					$lastType = $post->post_type;
					switch ($post->post_type)
					{
						case 'post':
							$postTypeTitle = "Blog";
							break;
						case 'page':
							$postTypeTitle = "Pages";
							break;
						case 'event':
							$postTypeTitle = "Events";
							break;
						case 'project':
							$postTypeTitle = "Projects";
							break;
						case 'newsletter':
							$postTypeTitle = "Newsletters";
							break;
					}

					echo '<h2>' . $postTypeTitle . '</h2><hr/>';
				endif;
		?>
				<h3><a href="<?=get_permalink()?>"><?php the_title(); ?></a></h3>
				<?php the_excerpt(); ?>
		<?php endwhile; ?>
	<?php else: ?>
		<p>No results found.</p>
	<?php endif; ?>

Enjoy!

mar 232012
 

This method automatically generates the canonical meta tag. It supports custom get / url parameters that would shift the page content. Insert on inside your layout’s head tag: <?php BC_Sysop::setCanonical($this); echo $this->headMeta(); ?> Class/method: <?php //BC_Sys::setCanonical written by Pedro Campos <pedrospdc@gmail.com> http://blog.psampaio.com.br class BC_Sys { static public function setCanonical(Zend_View &$view) { $request = Zend_Controller_Front::getInstance()->getRequest(); $filter [...]

 Posted by at 1:38
mar 222012
 

Helper class that manages and eases the use of breadcrumbs on Zend Framework, without Zend Navigator. It supports the automated use of params (url/get/post) to generate the breadcrumbs, dynamic breadcrumbs, and is fed via XML. Tested w/ v1.11. Class <?php //BC_Breadcrumb written by Pedro Campos (pedrospdc@gmail.com / http://psampaio.com.br) class BC_Breadcrumb { protected $_showHome = true; [...]

fev 182012
 

pSort is a jQuery plugin made with the purpose of being flexible enough to be used in ANY cases, still, remain pretty simple to be used. Actually it is shipped with a lexicographical sort algorithm, which means basically it’ll sort alphabetically or by value. More algorithms will be added in the future. With pSort you [...]

fev 132012
 

Classe para aplicar uma máscara em uma string, formatando-a. Similar à funcionalidade do plugin jQuery Mask. Ex: $bc_string = new BC_String(); $bc_string->setMascara(‘(99) 9999-9999′); $bc_string->mascarar(’1112345678′); //retorna (11) 1234-5678 ou $bc_string = new BC_String(); $bc_string->mascarar(’1112345678′, ‘(99) 9999-9999′); //retorna (11) 1234-5678   Máscaras aceitam os seguintes caracteres: 9 – Número a – Letra * – Letra e número [...]

dez 022011
 

Criei recentemente um script chamado JsPage, que na implementação de paginação de ítens enviados por Ajax. Não há alteração em códigos PHP – o paginador guarda todos os ítens em DOM, evitando lentidão na navegação e requisições no servidor. Pontos importantes: O script deixa apenas uma página por vez no código Se houverem imagens, o [...]

nov 092011
 

Série de snippets úteis para quem utiliza SVN por linha de comando.   Criar estrutura básica SVN svn mkdir branches svn mkdir tags svn mkdir trunk cd trunk svn mkdir data svn mkdir www Remover arquivos não encontrados localmente do repositório (arquivos mostrados como ! quando utilizado o comando svn status) svn status | grep [...]

nov 082011
 

Snippet para cortar strings/frases de acordo com um tamanho especificado ou de um objeto na página. Útil para criar “resumos” ou limitar tamanhos de links sem utilizar PHP, ou quando o tamanho das letras é dinâmico ou varia. Utiliza a biblioteca jQuery. $(‘.meu_link’).each( function(i) { // int – Tamanho maximo, pode ser setado manualmente wmax [...]