Excluding Posts from Blog Page in WordPress

Ok, this one seemed to trip me up quite a bit when I had a client ask to exclude a certain category from the main blog page in WordPress. For example, if you have a category of “news” or a category of “events” that you don’t want to show up on your main blog page you’ve designated in WordPress just add this snippet to your theme’s functions file at the bottom before the closing php tag “?>”,  if there is one in your theme.

// Exclude news and events category posts from specified blog page

function exclude_category($query) {
if ( $query->is_home() ) {
$query->set(‘cat’, ‘-6 -7’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘exclude_category’);

Note that in this case, category 6 was my “news” category and category “7” was my “events” category. So, in this query I’m simply prefixing the numerical id with a minus to exclude it from the results.

The full details can be found on the good ol’ WordPress Codex here – http://codex.wordpress.org/Class_Reference/WP_Query

Hopefully that’ll help some poor soul searching for this seemingly simple task out there.

Cheers!

Drupal Views PHP and Text Values

Man, after much tinkering and frustration I have finally realized that when using Views PHP to construct fields you cannot print actual text values out. Instead it will produce the entity number in your output code.

So, in case you are trying to get a Global PHP field to write out an actual value from your database using a $row variable it’s not going to work. You’ll end up with the entity number instead of the text you’re retrieving. Either you have to use a Global text field and available tokens or retrieve the raw text like this using $data:

<?php print ‘<p>This is the text here. And, here is your ‘ .$data->field_field_yourvalue[‘0’][‘raw’][‘value’]. ‘</p>’; ?>

Here’s some more detail on this if anyone is interested. http://drupal.org/node/1140896

Copyright Year Automatic Update

I hate having to go in and manually update the year in the footer on websites next to the copyright text. This isn’t anything fancy or new but just something to keep in your brain so you don’t have to go around updating the year every January in your website footers.

Simply use this little bit of PHP and it’ll update automatically:

<?php print date(‘Y’);?>

So, for example your html might look like this:

<p>&copy;<?php print date(‘Y’);?>, Josh Merriam, maker of websites. Don’t steal and stuff.</p>