Is it possible to create a function similar to this one
function add_category_automatically($post_ID) {
global $wpdb;
$postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
foreach ($postsWeWants as $postsWeWant) {
if(($postsWeWant->post_author != 30) && ($postsWeWant->post_author != 29) && !in_category('bundle')){
$cat = array(9547,9742);
wp_set_object_terms($post_ID, $cat, 'category', true);
}
}
}
add_action('publish_post', 'add_category_automatically');
but based on the post class instead of author and category?
something like that should do the trick:
function add_category_by_class($post_ID) {
if( in_array( 'classToCheckFor' get_post_class('', $post_ID)) ){
$cat = array(9547,9742);
wp_set_object_terms($post_ID, $cat, 'category', true);
}
}
add_action('publish_post', 'add_category_by_class');
Related
Some of my tags have some custom term meta and I'm saving that meta with this function:
add_action ( 'edit_term', 'save_termmeta_tag');
// save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
if ( isset( $_POST['Tag_meta'] ) ) {
$t_id = $term_id;
$tag_meta = get_option( "tag_$t_id");
$tag_keys = array_keys($_POST['Tag_meta']);
foreach ($tag_keys as $key){
if (isset($_POST['Tag_meta'][$key])){
$tag_meta[$key] = $_POST['Tag_meta'][$key];
}
}
//save the option array
update_option( "tag_$t_id", $tag_meta );
}
}
Now, I would like to get all tag with a specific option like 'channel'.
Is this possible, and how?
function get_term_with_option($opt) {
$terms = array();
// load all options
$options = wp_load_alloptions();
foreach($options as $k=>$v) {
// find keys with prefix `tag_`
if (strpos($k, 'tag_') === 0) {
$v = maybe_unserialize($v);
// check if has a meta key $opt
if (is_array($v) && array_key_exists($opt, $v)) {
$term = get_term(intval(str_replace('tag_', '', $k)));
if (!is_wp_error($term)) {
$terms[] = $term;
}
}
}
}
return $terms;
}
print_r(get_term_with_option('channel'));
I am wondering if there is any way to translate wordpress custom taxonomies on the frontend?
My idea is to intercept the rendering of taxonomies at some point (at my functions.php) and then render the translated strings which are stored in an array or JSON or txt file. I prefer not to use any plugin.
Your comments and and answers are welcome. Thanks.
add_filter('get_term', 'change_term_name_at_front', 10, 2);
function change_term_name_at_front($term, $taxonomy) {
//only for frontend
if( is_admin() ) {
return $term;
}
$new_names_array = array(
"taxonomy_id" => "New name",
"taxonomy_id2" => "New name2",
);
if ($taxonomy == 'your_taxonomy_name') {
$new_name = $new_names_array[$term->term_id];
if ($new_name !="") {
$term->name = $new_name;
}
}
return $term;
}
If you already have ACF plugin, you can add an additional field to the taxonomy and write there the new name, instead of using an array.
add_filter('get_term', 'change_term_name_at_front', 10, 2);
function change_term_name_at_front($term, $taxonomy) {
//only for frontend
if( is_admin() ) {
return $term;
}
if ($taxonomy == 'your_taxonomy_name') {
$key_field_acf = "yourtax_" . $term->term_id;
$new_name = get_field( 'new_name', $key_field_acf );
if ($new_name !="") {
$term->name = $new_name;
}
}
return $term;
}
I'm doing a dropdown in my post custom type just to filter the content when is completed or not.
my meta_value is a serialize object like "a:3:{s:5:"email";s:21:"mrsxcvsfesr#gmail.com";s:9:"store";s:6:"testes";s:5:"token";s:34:"$P$B7efpLZUWWyB4QkhG0YaYyIwXRAj.3.";}"
and my job is to check if the token is null or not
CustomPostTypeController.php
add_action('restrict_manage_posts',[ $this, 'dropdown_status_filter']);
add_filter( 'parse_query', [ $this, 'filter_request_query'])
public function dropdown_status_filter($post_type){
if('ctp_dasboard' !== $post_type) {
return; //filter your post
}
$selected = '';
$request_attr = 'status_filter';
if ( isset($_REQUEST[$request_attr]) ) {
$selected = $_REQUEST[$request_attr];
}
echo '<select id="status_filter-loc" name="status_filter">';
echo ($selected === '1') ? "'<option value='1' selected>Done</option>'" : "'<option value='1'>Done</option>'";
echo ($selected === '2') ? "'<option value='2' selected>Pending</option>'" : "'<option value='2'>Pending</option>'";
echo '</select>';
}
public function filter_request_query($query){
//modify the query only if it admin and main query.
if( !(is_admin() AND $query->is_main_query()) ){
return $query;
}
//we want to modify the query for the targeted custom post and filter option
if( !('ctp_dasboard' === $query->query['post_type'] AND isset($_REQUEST['status_filter']) ) ){
return $query;
}
//for the default value of our filter no modification is required
if(0 == $_REQUEST['status_filter']){
return $query;
}
//modify the query_vars.
// $value = "";
// var_dump($_REQUEST['status_filter'] === '1');
if ( $_REQUEST['status_filter'] === '1'){
// global $wpdb;
// $meta_key = 'form_ctp';
// $meta_value = '"token";N;';
// $mid = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value LIKE %s ", $meta_key, $meta_value) );
// dd($mid);
// // The Query
$args = [
'post_type' => 'ctp_dasboard',
['meta_query' => [
'key' => 'form_ctp',
'value' => 'token";N;', // this is when token is null
'compare' => 'IN'
]]
];
// dd($args);
$query = new \WP_Query( $args );
// dd($the_query);
}
if ( $_REQUEST['status_filter'] === '2'){
}
return $query;
}
There is a special function maybe_unserialize which can return you unserialized data array/ string and etc. Check documentation. Your variable should look like this:
$str = "a:3{s:5:"email";s:21:"mrsxcvsfesr#gmail.com";s:9:"store";s:6:"testes";s:5:"token";s:34:"$P$B7efpLZUWWyB4QkhG0YaYyIwXRAj.3.";}";
$data = maybe_unserialize($str); // will return an array
I have a custom orders posts and i wish to implement an additional filter for the list view (a select drop) in the admin menu.
I have been messing with this for hours but couldn't find a solution.
i wish to filter the posts by several meta keys and values and by post date
this is the code im using for this matter
add_filter( 'parse_query', 'order_posts_filter' );
function order_posts_filter( $query ){
global $pagenow,$wpdb;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'orders' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
$query->query_vars['post_date'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
if ( 'orders' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_STATUS_VALUE']) && $_GET['ADMIN_FILTER_STATUS_VALUE'] != '') {
$query->query_vars['meta_key'] = 'order_status';
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_STATUS_VALUE'];
}
return $query;
}
What am i doing wrong here ?
my $_GET parameters are working as expected and i am getting the relevant data from the select boxes.
thanks
I'm using this:
function wpse454363_posts_filter( $query ){
global $pagenow;
$type = 'post';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'product' == $type && is_admin() && $pagenow=='edit.php') {
$meta_query = array(); // Declare meta query to fill after
if (isset($_GET['post_date']) && $_GET['post_date'] != '') {
// first meta key/value
$meta_query[] = array (
'key' => 'post_date',
'value' => $_GET['post_date']
);
}
if (isset($_GET['order_status']) && $_GET['order_status'] != '') {
// second meta key/value
$meta_query[] = array (
'key' => 'order_status',
'value' => $_GET['order_status']
);
}
$query->query_vars['meta_query'] = $meta_query; // add meta queries to $query
}
}
add_filter( 'parse_query', 'wpse454363_posts_filter' );
need to update a custom field value if it is blank mis if it has any value not want to do anything
I want to update only if it is empty
code:
$custom_fields = get_post_custom($post_ID);//Current post id
$my_custom_field = $custom_fields['artist'];//key name
foreach ( $my_custom_field as $key => $value );
if(empty($value) or ($value == '')){
$artist_v = get_post_custom_values('artist', $post_ID);
update_post_meta($post_ID, 'artist', $parent_title, $artist_v);
}else{
//add_post_meta($post_ID, 'artist', $parent_title, true);
}
Below example might help:
`
if(!empty($ARR_meta_data))
{
foreach($ARR_meta_data as $meta)
{
if(trim($meta['meta_value']) == "")
{
update_postmeta_by_meta_id($meta['meta_id'], " YOUR CUSTOM VALUE ");
}
}
}
function get_postmeta_details_in_array($post_id, $meta_key)
{
global $wpdb;
$sql = "SELECT * FROM ".$wpdb->prefix."postmeta
WHERE meta_key='{$meta_key}' AND post_id='{$post_id}'";
return $wpdb->get_results($sql, ARRAY_A);
}
function update_postmeta_by_meta_id($meta_id, $meta_value)
{
global $wpdb;
$sql = "UPDATE ".$wpdb->prefix."postmeta SET meta_value='$meta_value'
WHERE meta_id='$meta_id'";
return $wpdb->query($sql);
}
?>
`
Place get_postmeta_details_in_array() and update_postmeta_by_meta_id() function in the functions.php in your wordpress theme.