1

The plugin search and filter, doesn't retrieve author_meta. So i need to add post_meta from author_meta. Is it possible?

EDIT

I create user_meta in this way:

    function custom_user_profile_fields($user){
    $previous_value = '';
    if( is_object($user) && isset($user->ID) ) {
        $previous_value = get_user_meta( $user->ID, 'company', true );
    }
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo esc_attr( $previous_value ); ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );

function save_custom_user_profile_fields($user_id){

    if(!current_user_can('manage_options'))
        return false;

    # save my custom field
    if( isset($_POST['company']) ) {
        update_user_meta( $user_id, 'company', sanitize_text_field( 
    $_POST['company'] ) );
    } else {
        //Delete the company field if $_POST['company'] is not set
        delete_user_meta( $user_id, 'company', $meta_value );
    }
    }
add_action('user_register', 'save_custom_user_profile_fields');
add_action( 'personal_options_update', 'save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_user_profile_fields' );

Now i need to set 'company' as a post_meta.

EDIT

I have added in functions.php your code, and i set every three minutes WP Cron but i didn't found MySql table 'genere' in postmeta. I have changed 'company', now is 'genere', but it's the same.

function  add_cron_recurrence_interval( $schedules ) {
$schedules['every_three_minutes'] = array(
        'interval'  => 180,
        'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
   return $schedules;
 }
 add_filter( 'cron_schedules', 'add_cron_recurrence_interval' );



 function author_company_post_sync() {
    global $wpdb;
      $posts= $wpdb->get_results("SELECT ID,post_author FROM ".$wpdb-
      >prefix."posts");
    foreach ($posts as $post) {
   $company = get_user_meta($post->post_author, 'genere', true);
 update_user_meta($post->ID, 'genere', $company);
   }
 }

 // schedule with WP Cron to run sync callback once daily...
 if (!wp_next_scheduled('author_company_post_sync')) {
    wp_schedule_event( time(), 'every_three_minutes', 
   'author_company_post_sync');
 }

EDIT

Here the solution: User meta to post

0

2 Answers 2

1

Yo might need the user meta for this

https://codex.wordpress.org/Function_Reference/get_user_meta

You can get to it by including the post author as the user_id.

So:

get_user_meta($post->post_author, 'meta_key'
1

You might be able to use a filter like this to get the matching author meta field (uses post metakey to check for an author metakey value):

add_filter('get_post_metadata', 'custom_get_author_meta', 10, 4);

function custom_get_author_meta($metadata, $post_id, $meta_key, $single) {
    if (!$metadata) {
        $post = get_post($post_id); 
        $author_id = $post->post_author;
        $value = get_the_author_meta($meta_key, $author_id);
        if ($value) {return $value;}
    }
    return $metadata;
}

Update: Specific answer for edited question:

add_filter('get_post_metadata', 'custom_get_author_company_meta', 10, 4);

function custom_get_author_meta($metadata, $post_id, $meta_key, $single) {
    if (!$meta_key == 'company') {
        $post = get_post($post_id); 
        $author_id = $post->post_author;
        $value = get_user_meta($author_id, 'company', true);
        if ($value) {return $value;}
    }
    return $metadata;
}

That should return the company field from the author's user meta field when you do the following:

$company = get_post_meta($postid, 'company', true);

So also if you are in the loop you can do:

global $post;
$company = get_post_meta($post->ID, 'company', true);

UPDATE: This alternative solution will loop all existing post records and sync the post_meta field 'company' with the author_meta key for the posts author of the same name... daily via WP Cron.

function author_company_post_sync() {
     global $wpdb;
     $posts= $wpdb->get_results("SELECT ID,post_author FROM ".$wpdb->prefix."posts");
     foreach ($posts as $post) {
          $company = get_user_meta($post->post_author, 'company', true);
          update_post_meta($post->ID, 'company', $company);
     }
}

// schedule with WP Cron to run sync callback once daily...
if (!wp_next_scheduled('author_company_post_sync')) {
    wp_schedule_event(time(), 'daily', 'author_company_post_sync');
}

This should make it compatible with any search plugin that wants the post_meta field specified because it will actually exist in that table now also. :-)

6
  • i have edited the question!
    – Andrea A
    Commented Jun 14, 2017 at 20:34
  • @AndreaA added update to the answer
    – majick
    Commented Jun 20, 2017 at 6:09
  • hi, i am sorry i answer now. I have add your code in functions.php but "Search and filter pro" still doesn't retrieve company. When i create a search filter, In the list of post_meta there isn't "company". Also, where do i have to put "$company = get_post_meta($postid, 'company', true);"? Maybe in functions.php? Thank you very much for your attention.
    – Andrea A
    Commented Jul 2, 2017 at 10:18
  • use the alternative code I've added in your functions.php instead, as a search plugin will typically not use WP meta functions but access the tables directly because it's much faster.
    – majick
    Commented Jul 8, 2017 at 2:59
  • Thank you very much for your attention. I have updated the queston!
    – Andrea A
    Commented Jul 11, 2017 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.