Exclude Category from Custom Post Type Loop / Archive - categories

I've been reading through posts on how to do this but none seem to make sense or work. I have created a custom post type (research) in my functions.php file and a custom taxonomy (classifications). I also have a custom archive page (archive-research.php) for my post type.
I have a category (classification) called "oldresearch" that I would like to exclude from the custom archive template.
Below is my custom post type code. Could someone please help me and let me know where the exclusion code needs to go.
<?php
/* redirect users to front page after login */
function redirect_to_front_page() {
global $redirect_to;
if (!isset($_GET['redirect_to'])) {
$redirect_to = get_option('siteurl');
}
}
add_action('login_form', 'redirect_to_front_page');
if ( function_exists('register_sidebars') )
register_sidebars(3);
add_action('init', 'register_custom_menu');
function register_custom_menu() {
register_nav_menu('custom_menu', __('Custom Menu'));
}
/** Registering Custom Post Type: Research **/
// Register Taxonomy for Research
$labels = array(
'name' => 'Classifications',
'singular_name' => 'Classification',
'search_items' => 'Search Classifications',
'popular_items' => 'Popular Classifications',
'all_items' => 'All Classifications',
'parent_item' => 'Parent Classifications',
'edit_item' => 'Edit Classifications',
'update_item' => 'Update Classifications',
'add_new_item' => 'Add New Classification',
'new_item_name' => 'New Classifications',
'separate_items_with_commas' => 'Separate Classifications with commas',
'add_or_remove_items' => 'Add or remove Classifications',
'choose_from_most_used' => 'Choose from most used Classifications'
);
$args = array(
'label' => 'Classifications',
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'research/classifications', 'with_front' => false ),
'query_var' => true
);
register_taxonomy( 'Classifications', 'Research', $args );
// Register Classification Column
add_filter( 'manage_research_posts_columns', 'ilc_cpt_columns' );
add_action('manage_research_posts_custom_column', 'ilc_cpt_custom_column', 10, 2);
function ilc_cpt_columns($defaults) {
$defaults['Classifications'] = 'Classifications';
return $defaults;
}
function ilc_cpt_custom_column($column_name, $post_id) {
$taxonomy = $column_name;
$post_type = get_post_type($post_id);
$terms = get_the_terms($post_id, $taxonomy);
if ( !empty($terms) ) {
foreach ( $terms as $term )
$post_terms[] = "<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " . esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
echo join( ', ', $post_terms );
}
else echo '<i>No terms.</i>';
}
// Register Custom Post Type
function research_post_type() {
$labels = array(
'name' => _x( 'Research', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Research', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Research', 'text_domain' ),
'parent_item_colon' => __( 'Parent Research', 'text_domain' ),
'all_items' => __( 'All Research', 'text_domain' ),
'view_item' => __( 'View Research', 'text_domain' ),
'add_new_item' => __( 'Add New Research', 'text_domain' ),
'add_new' => __( 'New Research', 'text_domain' ),
'edit_item' => __( 'Edit Research', 'text_domain' ),
'update_item' => __( 'Update Research', 'text_domain' ),
'search_items' => __( 'Search Research', 'text_domain' ),
'not_found' => __( 'No Research found', 'text_domain' ),
'not_found_in_trash' => __( 'No Research found in Trash', 'text_domain' ),
);
$rewrite = array(
'slug' => 'research',
'with_front' => true,
'rewrite_pages' => true,
'rewrite_feeds' => true,
);
$args = array(
'label' => __( 'research', 'text_domain' ),
'description' => __( 'Agri-Gro product research', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats', ),
'taxonomies' => array( 'Classifications', 'post_tag' ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'http://www.agrigro.com/news/wp-content/uploads/2013/01/Documents-icon.png',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'research', $args );
}
// Hook into the 'init' action
add_action( 'init', 'research_post_type', 0 );
?>

You can use the pre_get_posts filter in your functions.php in order to alter the query for a specific post_type. This is not the only way to achieve the task,but in my opinion it is quite easiest
UPDATE
You need to exclude a custom taxonomy term from your query so, we have to set a tax_query object, so:
Example:
add_action('pre_get_posts','custom_get_posts');
function custom_get_posts($query) {
// We are not displaying the posts on admin panel and this is the main query
if ( !is_admin() && $query->is_main_query() ) {
//Define the tax_query
$taxquery = array(
array(
'taxonomy' => 'Classifications', // also try lower case, remember a taxonomy name must be in lowercase
'field' => 'slug',
'terms' => array( 'oldresearch' ),
'operator' => 'NOT IN'
)
);
// We are displaying a custom posts type archive
if( $query->is_post_type_archive( 'research' ) ){
$query->set('tax_query', $taxquery );
}
}
}
Hope it helps! Let me know if you get stuck.

Related

applicable_formats for only the user 'My home'

What needs to be in the array in the applicable_formats() function so it only is available on the My home page of the user? I've set my-index to true.
acces.php
<?php
$capabilities = array(
'block/groups:myaddinstance' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'user' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/my:manageblocks'
),
'block/groups:addinstance' => array(
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => array(
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'moodle/site:manageblocks'
),
);
I've copied access.php from the blocks tutorial and only changed the names
UPDATE - it should be just 'my' rather than 'my-index'.
Also just for completeness, you might want to add all = false
public function applicable_formats() {
return array('all' => false, my' => true);
}

Create custom module without bean to show up in menu

I am wondering how to create custom module in SugarCRM and show link into the menu bar. So my custom module will not be a Bean, but needs to be visible in menu bar.
My manifest.php file is as follows:
$manifest = array(
'acceptable_sugar_versions' => array(
"rege_matches" => array("5.1.*")
),
'acceptable_sugar_flavors' => array(
'CE'
),
'name' => 'CustomModule',
'version' => '1.0',
'description' => 'CustomModule for SugarCRM',
'author' => 'Community',
'published_date' => '2015/02/17',
'type' => 'module',
'icon' => 'icons/default/icon_CustomModule.gif',
'is_uninstallable' => 'true',
);
$installdefs = array(
'id'=> 'CustomModule',
'copy' => array (
array (
'from' => '<basepath>/package/CustomModule',
'to' => 'modules/CustomModule',
),
),
'language' => array(
array(
'language'=> 'en_us',
'from'=> '<basepath>/package/language/application/en_us.lang.php',
'to_module' => 'application',
'language' => 'en_us',
),
),
);
Thanks!

How to put a picture instead of text in radio element in ZF2?

Subject: How to put a picture instead of text in radio element in ZF2?
code:
Options field:
array(
'spec' => array(
'type' => 'radio',
'name' => 'fcaptcha',
'options' => array(
'label' => 'Капча',
'label_attributes' => array(
'class' => 'control-label',
),
),
'attributes' => array(
'required' => 'required',
),
),
)
Controller:
$temp[0] = 'Here you need to put a picture instead of text';
$temp[1] = 'Here you need to put a picture instead of text';
$form->get('fcaptcha')->setValueOptions($temp);
You can do that just with some CSS. Here's a simple example that you can adapt for your purpose :
Let say you have this Zend\Form\Element\Radio element :
array(
'type' => 'Zend\Form\Element\Radio',
'name' => 'fcaptcha',
'options' => array(
'value_options' => array(
'0' => 'something1',
'1' => 'something2',
),
),
'attributes' => array(
'id'=>"fcaptcha-id"
)
)
CSS :
radio-option1 {background-image:url(option1.png);}
radio-option2 {background-image:url(option2.png);}
Some JS :
$(document).ready(function(){
$(':radio[value="0"]').addClass('radio-option1');
$(':radio[value="1"]').addClass('radio-option2');
});

how to use "filerenameupload" filter in zend framework2?

i add file element in form class with:
$this->add(array(
'type' => 'Zend\Form\Element\File',
'name' => 'logo_file',
'options' => array(
'label' => 'Select your logo image file:',
),
));
then add filter in model for filtering form data. i use "filerenameupload" filter to upload selected file :
$inputFilter = new InputFilter();
$inputFilter->add($factory->createInput(array(
'name' => 'logo_file',
'required' => false,
'filters' => array(
array('name' => 'filerenameupload',
'options'=>array(
//'target' => "./data/logo.png",
'randomize' => true,
)
),
),
)));
and in controller i call setInputFilter, setData and isValid normally. other elements go filter good, but "logo_file" does not be saved in "./data/logo.png".
in fact "filter" function in "Zend\Filter\File\RenameUpload" class does not be executed.
i use this link :
zf2 File Uploding Toturial
Someone's trying to solve this problem?
Have you tried using the full name for the Filter?
array(
'name' => 'Zend\Filter\File\RenameUpload'
)
You also need to make sure you add both the files array and the POST data to the form when validating, example:
$postArr = $request->getPost()->toArray();
$fileArr = $this->params()->fromFiles('logo_file');
$formData = array_merge(
$postArr, // $_POST
array('logo_file' => $fileArr['name']) // $_FILE...
);
$importForm->setData($formData);
Try:
Form.php:
public function addElements(){
$this->add(array(
'name' => 'image',
'attributes' => array(
'type' => 'file',
),
'options' => array(
),
));
FormValidator.php
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
"name" => "image",
"required" => true,
"filters" => array(
array("name" => "StripTags"),
array("name" => "StringTrim"),
array(
"name" => "Zend\Filter\File\RenameUpload",
"options" => array(
"target" => '/home/limonazzo/UPLOADDIR<----------',
"randomize" => true,
"use_upload_name" => true,
"use_upload_extension" => true
)
)
),
"validators" => array(
array(
"name" => "Zend\Validator\File\IsImage",
"break_chain_on_failure" => true,
"options" => array(
),
),
array(
"name" => "Zend\Validator\File\Extension",
"break_chain_on_failure" => true,
"options" => array(
"extension" => "jpg,jpeg,png",
),
),
array(
"name" => "Zend\Validator\File\Size",
"break_chain_on_failure" => true,
"options" => array(
"min" => "1kB",
"max" => "1024kB",
),
),
array(
"name" => "Zend\Validator\File\ImageSize",
"break_chain_on_failure" => true,
"options" => array(
"minWidth" => 10,
"minHeight" => 10,
"maxWidth" => 250,
"maxHeight" => 350,
),
),
),
)));
In controlle.php
$form = new Form();
$formValidator = new FormValidator();
$form->setInputFilter($formValidator->getInputFilter(''));
if ($form->isValid()) {
$data = $form->getData(\Zend\Form\FormInterface::VALUES_AS_ARRAY);
$imgName = $data["image"]["tmp_name"];

Zend_Dojo_Form passwordTextBox validator don't work

I am a newbie in zf, recently I m making a register form using zend_dojo_form and there are two passwordtextbox elements, which u know, one is for entering password and the other is to confirm the former one, then I using the validator 'token' but failed, here is part of my code.
$this->addElement('PasswordTextBox','password',array(
'label'=>'password:',
'required'=>true,
'trim'=>true,
'regExp'=>'^[a-z0-9]{6,18}$',
'invalidMessage'=>'password should be 6-18',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
//$this->addElement($password1);
$this->addElement('PasswordTextBox','password2',array(
'label'=>'confirm password:',
'required'=>true,
'trim'=>true,
//'regExp'=>'^[a-z0-9]{6,18}$',
'validators'=>array(array('identical',false,array('token'=>'password'))),
'invalidMessage'=>'the password you enter not the same',
'Decorators' => array(
'DijitElement',
'Errors',
array(array('data'=>'HtmlTag'),array('tag'=>'td','align'=>'left')),
array('Label',array('tag'=>'td')),
array(array('row'=>'HtmlTag'),array('tag'=>'tr','align'=>'right'))
)
)
);
Here's a sample that should be functional:
class Application_Form_Register extends Zend_Dojo_Form {
public function init() {
$this
->setOptions(array('name' => 'registerForm'))
// username
->addElement('TextBox', 'first_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'name_register',
'required' => true,
'label' => 'Fornavn(e):',
))
->addElement('TextBox', 'last_name', array(
'filters' => array('StringTrim', 'StringToLower'),
'id' => 'surname_register',
'label' => 'Efternavn:',
))
->addElement('ValidationTextBox', 'email', array(
'filters' => array('StringTrim', 'StringToLower'),
'validators' => array(
'EmailAddress',
array('StringLength', false, array(3, 60)),
),
'regExp' => "^(([^<>()[\]\\.,;:\s#\"]+(\.[^<>()[\]\\.,;:\s#\"]+)*)|(\".+\"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$",
'id' => 'email_register',
'required' => true,
'label' => 'E-mail:',
))
//password
->addElement('PasswordTextBox', 'password', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'regExp' => "^([a-zA-Z0-9_\-!-=]){4,32}$",
'tooltipPosition' => 'above',
'invalidMessage' => 'Krav til password: mellem 4 og 32 i længde og kun karakterer som tal, bogstaver eller blandt [ _-!"#%&/()=] er tilladt',
'id' => 'password_register',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register1',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('PasswordTextBox', 'password_confirm', array(
'filters' => array('StringTrim'),
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
'id' => 'passwordconfirm_register2',
'onKeyDown' => 'arguments[0].keyCode == dojo.keys.ENTER && application.submitRegistration(dijit.byId(\'registerForm\'));',
'required' => true,
'label' => 'Gentag password:',
))
->addElement('Hidden', 'action', array(
'id' => 'action_register',
'value' => 'register',
))
->addElement('Button', 'foo', array(
'id' => 'foo_register',
'onClick' => 'application.submitRegistration(dijit.byId(\'registerForm\'))',
'label' => 'Register'
)
);
$this->_decorate();
}
// renders nulled empty labels and adds classnames, related to element name
protected function _decorate() {
$this->setDecorators(array(
'FormElements',
array('HtmlTag', array('tag' => 'div', 'class' => 'zend_form_contents')),
'DijitForm'
));
foreach ($this->getElements() as $el) {
$el->addDecorator('HtmlTag', array('tag' => 'div', 'class' => $el->getName() . '-wrap'));
if ($el->helper == 'Button')
continue;
else if (strlen(trim($el->getLabel())) == 0)
$el->addDecorator('Label', array('tag' => null));
else
$el->addDecorator('Label', array('tag' => 'div', 'class' => $el->getName() . '-label'));
}
}
}
Focusing on the two password fields - in terms of what makes clientside validation tick, lets filter out the Zend_Form_Element attributes:
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
'Alnum',
array('StringLength', false, array(6, 20)),
),
// widget id
'id' => 'passwordconfirm_register1',
// both client-/server-side - triggers 'emptyMessage' validation
'required' => true,
// runs with the form.validate() and events onblur, oninit, onkeypress
// if ! dijit.get("value").match(dijit.regExp) show 'invalidMessage'
// this example matches the serverside reqs
'regExp' => "^([a-zA-Z0-9_\-!-=]){6,32}$",
// shown with form.validate(), dijit.validate() and input.blur() if regExp is not a match against value
'invalidMessage' => 'Password musts: 6 to 32 chars and only numbers, letters or amongst [ _-!"#%&/()=]',
))
And a second pwd field, same attributes but with a different validator
->addElement('PasswordTextBox', 'password_2_', array(
// strictly serverside, only accepts alphanumerical pwds lengths 6 through 20
'validators' => array(
array('identical',false,array('token'=>'password'))
),
// widget id
'id' => 'passwordconfirm_register2',
'required' => true,
// in theory, we would define 'validator' in programmatic construct of
// the validationtextbox. but this will not work with the way Zend handles dojo
// 'validator' => 'function(value, constraints) { return "BAD PRACTICE" }'
));
// add javascript code to run onload and to extend the 'register2' properly
$this->getElement()
->getView()
->headScript()
->appendFile($baseUrl . '/js/Validator.js');
By now, only 'required' is validated clientside in the passwordconfirm_register2 widget. Lets fix in what markup factory does in regards to 'bad practice' in the above lines. Remove that line and add the javascript code instead, containing:
// file: baseUrl/js/Validator.js
dojo.addOnLoad(function() {
dijit.byId('passwordconfirm_register2').validate = function(value, constraints) {
if(dijit.byId('passwordconfirm_register1').get("value") != value) {
this.invalidMessage = "Passwords are not identical";
return false;
}
return true
});
});