I have a form that shows a drop-down menu to select a single value from an array defined in params.yml.
The dropdown displays '0' as an optgroup label above the list of values to be selected.
How can I get rid of this default label ?
You can replace the default OptGroup with something like the "Standard Hours" and "Other Hours" as defined below.
$category_choices = array(
'Standard Hours' => array(
2 => '2',
4 => '4',
6 => '6',
8 => '8'
),
'Other Hours' => array(
1 => '1',
3 => '3',
5 => '5',
7 => '7',
9 => '9',
10 => '10'
)
);
$builder
->add('hours', 'choice', array(
'choices' => $category_choices
));
Alternatively, if you don't want OptGroup labels at all, and only selectable entries, you can just skip out on the nested arrays altogether.
$builder
->add('hours', 'choice', array(
'choices' => array(
1 => '1',
2 => '2',
3 => '3',
4 => '4'
)
));
In your case, you want something like:
$builder
->add('hours', 'choice', array(
'choices' => arrayFromParamsYML
));
Related
I have created a WP Query argument list like so hoping it will select all posts with term 2 and then from results all others with relation OR.
$similar_properties_args = array(
'post_type' => 'property',
'posts_per_page' => '10',
'post__not_in' => array(get_post_ID() ),
'post_parent__not_in' => array(get_post_ID() ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'language',
'terms' => '2'
),
array (
'relation' => 'OR',
array(
array(
'taxonomy' => 'property-feature',
'field' => 'id',
'terms' => array(1954,1958,1966,1970,1972)
),
array(
'taxonomy' => 'property-city',
'field' => 'id',
'terms' => array(145)
),
array(
'taxonomy' => 'property-status',
'field' => 'id',
'terms' => array(38)
),
array(
'taxonomy' => 'property-type',
'field' => 'id',
'terms' => array(21)
)
)
)
)
);
The actual WP mysql query created is as as below:
SELECT SQL_CALC_FOUND_ROWS agn_posts.ID FROM agn_posts LEFT JOIN agn_term_relationships ON (agn_posts.ID = agn_term_relationships.object_id) LEFT JOIN agn_term_relationships AS tt1 ON (agn_posts.ID = tt1.object_id) LEFT JOIN agn_term_relationships AS tt2 ON (agn_posts.ID = tt2.object_id) LEFT JOIN agn_term_relationships AS tt3 ON (agn_posts.ID = tt3.object_id) LEFT JOIN agn_term_relationships AS tt4 ON (agn_posts.ID = tt4.object_id) WHERE 1=1 AND agn_posts.ID NOT IN (1440) AND agn_posts.post_parent NOT IN (1440) AND (
agn_term_relationships.term_taxonomy_id IN (2)
AND
(
(
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972)
AND
tt2.term_taxonomy_id IN (145)
AND
tt3.term_taxonomy_id IN (38)
AND
tt4.term_taxonomy_id IN (21)
)
)
) AND agn_posts.post_type = 'property' AND (agn_posts.post_status = 'publish' OR agn_posts.post_status = 'private') GROUP BY agn_posts.ID ORDER BY agn_posts.post_date DESC LIMIT 0, 10
...and it is not giving me the results as I would need to get.
If I change the tt1 term relationship to OR, then the result is fine.
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972)
OR //from AND to OR
tt2.term_taxonomy_id IN (145)
OR //from AND to OR
tt3.term_taxonomy_id IN (38)
OR //from AND to OR
tt4.term_taxonomy_id IN (21)
The problem Is, from my naked eye, the argument list above is valid for creating the query I am looking for, but somehow something is missing. May be you can see, where is the problem in my code?
You shouldn't nest the four arrays/clauses in an array:
'tax_query' => array(
'relation' => 'AND', // for clauses 1 and 2
array( // clause 1
'taxonomy' => 'language',
'terms' => '2',
),
array( // clause 2
'relation' => 'OR', // for sub-clause 1..
// you shouldn't nest the sub-sub-clauses
array( // sub-clause 1
array( // sub-sub-clause 1
'taxonomy' => 'property-feature',
...
),
array( // sub-sub-clause 2
'taxonomy' => 'property-city',
...
),
array( // sub-sub-clause 3
'taxonomy' => 'property-status',
...
),
array( // sub-sub-clause 4
'taxonomy' => 'property-type',
...
),
),
),
),
Instead, make the arrays same level as the relation:
'tax_query' => array(
'relation' => 'AND', // for clauses 1 and 2
array( // clause 1
'taxonomy' => 'language',
'terms' => '2',
),
array( // clause 2
'relation' => 'OR', // for the sub-clauses 1, 2, 3 and 4
// instead of nesting, do like so:
array( // sub-clause 1
'taxonomy' => 'property-feature',
'terms' => array( 1954, 1958, 1966, 1970, 1972 ),
),
array( // sub-clause 2
'taxonomy' => 'property-city',
'terms' => array( 145 ),
),
array( // sub-clause 3
'taxonomy' => 'property-status',
'terms' => array( 38 ),
),
array( // sub-clause 4
'taxonomy' => 'property-type',
'terms' => array( 21 ),
),
),
),
And that one would give you this SQL command as part of the WHERE:
agn_term_relationships.term_taxonomy_id IN (2) # language
AND
(
tt1.term_taxonomy_id IN (1954,1958,1966,1970,1972) # feature
OR
tt1.term_taxonomy_id IN (145) # city
OR
tt1.term_taxonomy_id IN (38) # status
OR
tt1.term_taxonomy_id IN (21) # type
)
And BTW, 'field' => 'id' should be 'field' => 'term_id', but the default field is indeed term_id, so you may omit the field arg. Also, WordPress does not have a function named get_post_ID, so I think you meant to use get_the_ID()? :)
I'm trying to implement a filter on wp_query to show annual archives by School Year (sept - may) rather than jan-dec. When I view a yearly archive page, such as sitename.com/2017/, I get a white screen of death, and no PHP errors.
My code in functions.php looks like the following:
add_action( 'pre_get_posts', 'school_year_filter' );
function school_year_filter($query) {
if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {
$year = get_query_var('year');
$year2 = $year + 1;
$query->set(
'date_query' => array(
array(
'after' => array(
'year' => "$year",
'month' => '9',
'day' => '1',
),
'before' => array(
'year' => "$year2",
'month' => '5',
'day' => '31',
),
'inclusive' => true,
),
),
);
}
}
Try this code.
I changed this line 'date_query'=>array( to 'date_query', array(.
add_action( 'pre_get_posts', 'school_year_filter' );
function school_year_filter($query) {
if (! is_admin() && $query->is_main_query() && $query->is_year() && $query->is_archive()) {
$year = get_query_var('year');
$year2 = $year + 1;
$query->set(
'date_query', array(
array(
'after' => array(
'year' => $year,
'month' => '9',
'day' => '1',
),
'before' => array(
'year' => $year2,
'month' => '5',
'day' => '31',
),
'inclusive' => true,
),
)
);
}
}
https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Only_Display_Search_Results_After_Specific_Date
I have 100 posts all have a meta_key named "isactive" whose value will either be "1" or "0"
I need to calculate the percentage of number of active posts(from 'isactive' metakey value) between any 2 dates
$args0 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 0
)
);
$count0 = new WP_Query( $args0 );
$args1 = array(
'date_query' => array(
array(
'after' => 'January 1st, 2013',
'before' => array(
'year' => 2013,
'month' => 2,
'day' => 28,
),
'inclusive' => true,
),
'meta_key' => 'isactive',
'meta_value' => 1
)
);
$count1 = new WP_Query( $args1 );
echo $percent = $count0/$count1;
Whether this type of query works ?
Else is there any better way to calculate percentage based on the meta key values between 2 dates?
I have "product" custom post type. It have 2 custom fields: price1, price2 (price of product)
I want to get all posts have price1 < price2 (compare 2 values)
How can i do that?
Here's a nice sample that should help you out:
$args = array(
'tax_query' => array(
'taxonomy' => 'custom_taxonomy_name',
array(
'key' => 'price',
'value' => array( 100, 200 ),
'compare' => 'BETWEEN',
'type' => 'numeric',
),
array(
'key' => 'description',
'value' => 'round',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query($args);
I have two model name image and comment. here the relation is like image has many comments . Now in my listing page I want to display all imae detail and only the no of comment on that image . can you tell me how should I get that ?
after I write the query my return data is
Array
(
[0] => Array
(
[Image] => Array
(
[image_id] => 57
[user_id] => 1
[category_id] => 22
[image_title] => scroul
[description] => beutifull natural image for the animal
[keyword] => scrual
[image_price] =>
[image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 12
[size] => 3244
[resolution] => 2162 x 1644
[i_date] => 1348573022
[i_by] => 1
[u_date] => 1348573022
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 5
[image_id] => 57
[user_id] => 2
[comment] => socute
[comment_date] => 1348739230
[is_active] => N
[is_deleted] => N
)
)
)
[1] => Array
(
[Image] => Array
(
[image_id] => 56
[user_id] => 1
[category_id] => 22
[image_title] => cute dog
[description] => cute dog looking
[keyword] =>
[image_price] =>
[image_name] => d4af899b0d52cccbec94952a3abd0077.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 8
[size] => 620
[resolution] => 2592 x 1944
[i_date] => 1348572897
[i_by] => 1
[u_date] => 1348572897
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 3
[image_id] => 56
[user_id] => 2
[comment] => ohhhhhhh
[comment_date] => 1348737968
[is_active] => N
[is_deleted] => N
)
)
)
[3] => Array
(
[Image] => Array
(
[image_id] => 55
[user_id] => 1
[category_id] => 22
[image_title] => ships
[description] => ships with beutiful green background
[keyword] => ship,green,animal,nature,background,eating,white ship
[image_price] =>
[image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 1
[size] => 1831
[resolution] => 2520 x 1944
[i_date] => 1348572846
[i_by] => 1
[u_date] => 1348661976
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 2
[image_id] => 55
[user_id] => 2
[comment] => i like it
[comment_date] => 1348737942
[is_active] => Y
[is_deleted] => N
)
[1] => Array
(
[comment_id] => 4
[image_id] => 55
[user_id] => 2
[comment] => good scene
[comment_date] => 1348738004
[is_active] => N
[is_deleted] => N
)
)
)
)
in the above array there is a all comment of that image. I don't want here comment list I just want no of comments .
Could you post the find statement you're using. You can set recursive to a certain level, from the documentation:
-1 Cake fetches Group data only, no joins.
0 Cake fetches Group data and its domain
1 Cake fetches a Group, its domain and its associated Users
2 Cake fetches a Group, its domain, its associated Users, and the Users’ associated Articles
So you can call the following if you only want the data from the current model only:
$this->Model->find('all', array('recursive' => -1));
There is also the Containable behavior, which allows you to specify which Model data you want to retrieve when calling find.
Say you have a Post model which hasMany Image. The call, with the Containable behaviour in the Post Model properly included, would be:
$this->Post->find('all', array(
'conditions' => array('Post.id' => 1),
'contain' => array('Image')
));
EDIT:
Because of the spelling and formatting I misread your initial question. I thought you wanted "no comments" to appear in the data array, instead you want only the "number of comments" to appear.
If you want the comment count, use counterCache as Kishor Kundan proposes.
Cake offers another amazing magic, "counterCache".
You can define the counterCache in your Comments model
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true
);
Then you add a field, 'comment_count' in your images table (or the table which is being used by the model Image) and the cake will do the rest for you.
This does add an overhead every time a comment is added/deleted but it is far better alternative than to issue an 'count' every time you fetch image data.
For more info you can check the cookbook. Look out for "counterCache" there.
UPDATE:
To limit the scope for counter cache, use additional attribute 'counterScope' as
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true,
'counterScope' => array('Image.active' => 1)
);