php.ini设置Max Input Vars和WordPress allowed memory size

Increasing Max Input Vars

Increasing Max Input Vars in .htaccess File

It’s possible to increase it from .htaccess file in root WordPress installation’s directory. For this you need connect to your server via FTP (using some FTP client like FileZilla), find .htaccess file in the root of your site directory (or create it yourself if it’s missing) and add below lines of codes at the end of the .htaccess file:

php_value max_input_vars 3000
php_value suhosin.get.max_vars 3000
php_value suhosin.post.max_vars 3000
php_value suhosin.request.max_vars 3000

Increasing Max Input Vars in php.ini File

For this scenario you should also connect via FTP or log in to the CPanel of your hosting provider.

At the end of the php.ini file, add below lines of codes. The php.ini file can be found in /user/local/bin/, /etc/php5/ or … depending on the host provider. If you can not find it, it’s better contact your hosting provider. On some hosting providers, you can create a php.ini file in the root directory and just add the following codes to override the default values.

max_input_vars = 3000
suhosin.get.max_vars = 3000
suhosin.post.max_vars = 3000
suhosin.request.max_vars = 3000

Increase the WordPress allowed memory size

To increase the WordPress allowed memory size for your website:

  1. Open wp-config.php, which by default is located in the root WordPress directory.
  2. Find the following line near the end of the file:
  3. /* That’s all, stop editing! Happy blogging. */
  4. Just above that line, add the following line:
  5. define(‘WP_MEMORY_LIMIT’, ’96M’);

Save your changes.

You can also increase memory to a higher number, and you can also increase the memory limit for admin tasks, as described in the WordPress codex.

If you’re still receiving the error after this fix, contact your web host provider or server administrator for their help.

eed插件设置货币为人民币的方法

Easy digital downloads 插件是款优秀的资源类下载插件,但是他默认的货币里面是没有人民币选项的,而且支付的网关里面也没有支付宝和微信支付的选项。

如果要在货币里面添加人民币支付的选项的话,通过以下方式就可以实现:

function rmb_extra_edd_currencies( $currencies ) {
	$currencies['RMB'] = __('Chinese Yuan(¥)', 'your_domain');
	return $currencies;
}
add_filter('edd_currencies', 'rmb_extra_edd_currencies');

自定义字段创建标签并在首页进行自定义标签类型的调用

比方说你要给自定义文章类型product创建一个类型标签,比如说置顶,之类的功能,你首先去custom field, 然后add field group, 然后add field, 选项设置为checkbox. 设置输入三种选项,featured, normal,hot sale. 进行分类。

如何在首页调用自定义标签为featured的文章呢?

要用到wordpress的自定义查询:

$posts = get_posts(array(
    'meta_query' => array(
        array(
            'key'     => 'colors',
            'value'   => '"red"',
            'compare' => 'LIKE'
        )
    )
));

关于acf插件checkbox属性调用进行文章分类的方法,具体见官方连接:

wordpress首页如何展示其他页的标题和内容

wordpress 函数get_post( int|WP_Post|null $post = null, string $output = OBJECT, string $filter = ‘raw’ ), 展现特定post的页面或者帖子数据。get_page_by_title( string $page_title, string $output = OBJECT, string|array $post_type = ‘page’ ) 获取特定title的页面,如get_page_by_title('World Peace Now', OBJECT, 'link'); 如果要获取页面标题为about us的页面内容的话,用法如下:

get_post(get_page_by_title(‘About Us’));

展现的内容为:

object(WP_Post)#871 (24) { [“ID”]=> int(21) [“post_author”]=> string(1) “1” [“post_date”]=> string(19) “2020-11-13 12:27:10” [“post_date_gmt”]=> string(19) “2020-11-13 12:27:10” [“post_content”]=> string(512) “

浙江景祥是一家专业研发、生产、销售制冷换热设备系列产品的综合性制造企业,为家用空调、商用空调、汽车空调、船用空调、新能源、轨道交通、工业制冷、冷冻冷藏等领域提供专业化、系统化的制冷解决方案和设备配套,年生产能力达60万套。公司现为国家高新技术企业、浙江省科技型企业、AAA级信用企业和诚信经营示范单位。” [“post_title”]=> string(8) “About Us” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish” [“comment_status”]=> string(6) “closed” [“ping_status”]=> string(6) “closed” [“post_password”]=> string(0) “” [“post_name”]=> string(8) “about-us” [“to_ping”]=> string(0) “” [“pinged”]=> string(0) “” [“post_modified”]=> string(19) “2020-11-14 13:46:06” [“post_modified_gmt”]=> string(19) “2020-11-14 13:46:06” [“post_content_filtered”]=> string(0) “” [“post_parent”]=> int(0) [“guid”]=> string(33) “http://www.method.top/?page_id=21” [“menu_order”]=> int(0) [“post_type”]=> string(4) “page” [“post_mime_type”]=> string(0) “” [“comment_count”]=> string(1) “0” [“filter”]=> string(3) “raw” },

如果要调取about us页面的内容的话,用法为:

$about = get_post(get_page_by_title(‘About Us’)); echo $about->post_content;

还有一种选取页面的方式为:get_page_by_path().

另一种获取页面内容的方式为:

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content; 

中文字符要截取特定字数的话写法是:echo mb_strimwidth(get_the_excerpt(),0,100,’ …..’),

当然,你也可以用wp_query来创建自定义查询到page的页面,用法如下:

Display post by ID:

$query = new WP_Query( array( 'p' => 7 ) );

Display page by ID:

$query = new WP_Query( array( 'page_id' => 7 ) );

Show post/page by slug

$query = new WP_Query( array( 'name' => 'about-my-life' ) );

Display page by slug:

$query = new WP_Query( array( 'pagename' => 'contact' ) );

Display child page using the slug of the parent and the child page, separated by a slash (e.g. ‘parent_slug/child_slug’):

$query = new WP_Query( array( 'pagename' => 'contact_us/canada' ) );

Display child pages using parent page ID:

$query = new WP_Query( array( 'post_parent' => 93 ) );

Display only top-level pages, exclude all child pages:

$query = new WP_Query( array( 'post_parent' => 0 ) );

Display posts whose parent is in an array:

$query = new WP_Query( array( 'post_parent__in' => array( 2, 5, 12, 14, 20 ) ) );

Display only the specific posts:

$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );

Display all posts but NOT the specified ones:

$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );

Note: you cannot combine post__in and post__not_in in the same query.

Also note that using a string containing a comma separated list will not work here. If you’re passing a variable, make sure it’s a proper array of integer values:

// This will NOT work
$exclude_ids = '1,2,3';
$query = new WP_Query( array( 'post__not_in' => array( $exclude_ids ) ) );

// This WILL work
$exclude_ids = array( 1, 2, 3 );
$query = new WP_Query( array( 'post__not_in' => $exclude_ids ) );

几个具体的参数:
p (int) – use post id.
name (string) – use post slug.
page_id (int) – use page id.
pagename (string) – use page slug.
post_parent (int) – use page id to return only child pages. Set to 0 to return only top-level entries.
post_parent__in (array) – use post ids. Specify posts whose parent is in an array. (available since version 3.6)
post_parent__not_in (array) – use post ids. Specify posts whose parent is not in an array. (available since version 3.6)
post__in (array) – use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
post__not_in (array) – use post ids. Specify post NOT to retrieve.
post_name__in (array) – use post slugs. Specify posts to retrieve. (Will be available in version 4.4)

Html怎样设置点击按钮进行下载

实现点击按钮进行文件下载我们要用到Html5的download标签或者js的自定义函数。

方法一:利用html5的download标签

download标签通利用a标签来储存需要下载的文件的位置,在download的value里面可以设置文件的名称,如果没有设置,a标签就会利用原始的文件。

用法:
<a download="filename">
filename就是你需要下载的文件的名称。
示例:
<!DOCTYPE html> 
<html> 
   <body> 
      <style> 
         p { 
         color: green; 
         } 
      </style> 
      <p>How to trigger a file download when  
         clicking an HTML button or JavaScript? 
      <p> 
         <!-- GFG is the name of the  
            file to be downloaded-->
         <!-- In order to run the code,  
            the location of the file  
            "geeksforgeeks.png" needs to  
            be changed to your local directory, 
            both the HTML and downloadable file  
            needs to be present in the same directory -->
         <a href="geeksforgeeks.png" download="GFG"> 
         <button type="button">Download</button> 
         </a> 
   </body> 
</html> 

方法二:利用js的自定义函数

  • 首先创建一个文本区域(textarea),所有的文本输入都将被发出。
  • 利用createElement属性创建a标签然后分配他的download和href属性。
  • encodeURIComponent将对所有具有特殊含义的内容进行编码,因此您可以将其用于uri的组件。

例如,如果我们有类似“Hello:Geek”的文本?“”中有特殊字符,因此encodeURIComponent将对它们进行编码并附加它以供进一步使用。

  • data:text/plain; charset=utf-8 是href的属性值(如href=“”),它指定该值必须是文本类型,并且具有utf-8类型编码。click()方法模拟鼠标单击元素。
  • 之后,我们只需使用来自textarea的文本和我们的文件名调用下载函数”GFG.txt文件“作为id为’btn’的输入按钮上的参数。
示例:
<!DOCTYPE html> 
<html> 
   <body> 
      <style> 
         p { 
         color: green; 
         } 
      </style> 
      <p> 
          How to trigger a file download when 
          clicking an HTML button or JavaScript? 
      <p> 
         <textarea id="text"> 
             Welcome to GeeksforGeeks 
         </textarea> 
         <br/> 
         <input type="button" id="btn" 
                value="Download" /> 
         <script> 
            function download(file, text) { 
              
                //creating an invisible element 
                var element = document.createElement('a'); 
                element.setAttribute('href',  
                'data:text/plain;charset=utf-8, ' 
                + encodeURIComponent(text)); 
                element.setAttribute('download', file); 
              
                // Above code is equivalent to 
                // <a href="path of file" download="file name"> 
              
                document.body.appendChild(element); 
              
                //onClick property 
                element.click(); 
              
                document.body.removeChild(element); 
            } 
              
            // Start file download. 
            document.getElementById("btn") 
            .addEventListener("click", function() { 
                // Generate download of hello.txt  
                // file with some content 
                var text = document.getElementById("text").value; 
                var filename = "GFG.txt"; 
              
                download(filename, text); 
            }, false); 
         </script> 
   </body> 
</html> 

Windows服务器通过xampp发送邮件

Xampp是一个集成的开发环境,主要的目的是为开发者提供本地开发环境。

在开发的过程当中,开发者基本上都需要利用xampp来接受邮件,需要能随时与网站的客户进行对话来避免不必要的生意的错失。

设置从xampp发送邮件,需要在xampp安装好之后进行配置。按照以下几个步骤就可以完成。

利用Xampp发送邮件

  1. 打开Xampp的安装目录。
  2. 定位到C:\xampp\php然后打开php.ini文件。
  3. 按住ctrl + f 搜索 [mail function]。
  4. 找到下面几个并进行替换。腾讯云和阿里云的服务器的邮件端口是465,因为服务器封了传统的25端口,由于一开始垃圾邮件泛滥的原因。

SMTP=smtp.qq.com
smtp_port=465
sendmail_from = yourname@qq.com
sendmail_path = “\”C:\xampp\sendmail\sendmail.exe\” -t”

接下来,定位到C:\xampp\sendmail文件夹,打开sendmail.ini进行配置。

  1. 按住ctrl + f 找到[sendmail]。
  2. 找到并替换掉下面内容:

smtp_server=smtp.qq.com
smtp_port=465
error_logfile=error.log
debug_logfile=debug.log
auth_username=YourGmailId@qq.com
auth_password=Your-Password(注意,这里是QQ邮箱的授权码,在QQ邮箱网页版,账户设置里,找到imap那里,点击生成授权码)
force_sender=YourGmailId@qq.com(optional)

测试邮件发送的简单的脚本:

<?php
$to_email = “receipient@gmail.com”;
$subject = “Simple Email Test via PHP”;
$body = “Hi,nn This is test email send by PHP Script”;
$headers = “From: sender\’s email”;

if (mail($to_email, $subject, $body, $headers)) {
    echo “Email successfully sent to $to_email…”;
} else {
    echo “Email sending failed…”;
}

如果你仍然还遇到邮件发送的问题,可能是以下几个原因:

  1. 邮件发送的比较慢,遇到网络拥堵。
  2. 邮件配置的不正确。
  3. 发送邮件的服务器端口被屏蔽了。
  4. 邮箱设置的授权码不正确,你设置成邮箱密码了。

wordpress展示和美化post meta data

Have you ever noticed a date or category in a blog post? This is what’s called a ”post meta data” – an important part of a blog post. It helps in providing succinct meta data regarding your blog’s content pieces.

If used properly post meta can improve user experience; say a visitor is interested in a certain topic only, then category meta can help them short posts by category so they can view posts on that particular topic rather than see all the posts. Or, if you want to view posts of a particular author only, post meta can help you do that.

In this post, I’ll show you how to display and style post meta data.

What is Post Meta Data and How Can it Help my Blog?

The post meta data section contains relevant information of a blog post such as published date, author name, categories, tags and custom taxonomies etc.

If you have a blog then you should make sure your post’s meta data is spot on, as this information can help a visitor understand more about the post and it can also help to increase your page views by making your site easier for navigation.

How Much Post Meta Data Should you Display?

The post meta data location varies from theme to theme. Some may display it before the post title, some after the title and some right after the content.

Post Meta Data

But too many post meta pieces can mess up the layout. The ideal situation is one in which you would only display the information you feel is necessary.

Now let’s see how you can customize and add post meta.

Customizing Post Meta Data

As mentioned, the post meta location varies from theme to theme. Here we will work on a particular theme – in this case the default TwentySeventeen theme, so keep in mind that code and pages may be different from your theme.

In modern themes post meta are defined in template tags page and called in when required but in some themes you may find post meta is placed directly before or after the post title.

Generally, you would find post meta tags in index.phpsingle.phparchive.php and content template pages.

A simple code would look like this:

Posted on

 <?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?> <?php edit_post_link(); ?>

This code will display something like this Posted on date by author name with edit button (only if you are logged in the dashboard).

But now most modern themes are using template tags page to handle post meta. Let’s see how it works.

First, you would have to create a child theme before making any changes.

We will work on single post page and this is how it looks from the front-end.

Post Meta from front-end

Let’s say we want to add a user icon before the author name, to do that do the following:

In TwentySeventeen theme, single.php template is used to display single post. Now look for get_template_part and notice the template path there, in our case the path is:

get_template_part( 'template-parts/post/content', get_post_format() );

Now according to the path open content.php and search for entry-header section.

<header class="entry-header">

<?php

if ( 'post' === get_post_type() ) {

echo '<div class="entry-meta">';

if ( is_single() ) {

twentyseventeen_posted_on();

} else {

echo twentyseventeen_time_link();

twentyseventeen_edit_link();

};

echo '</div><!-- .entry-meta -->';

};

 

if ( is_single() ) {

the_title( '<h1 class="entry-title">', '</h1>' );

} elseif ( is_front_page() && is_home() ) {

the_title( '<h3 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h3>' );

} else {

the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );

}

?>

</header>

<!-- .entry-header -->

We can see twentyseventeen_posted_on(); function is called here to display the post meta, we have to edit this function. You will find this function in template-tags.php file.

A tip:  check theme’s function.php file to get the file name and path.

Search for twentyseventeen_posted_on() and copy paste the entire function in your child theme’s function.php file (if you don’t have function.php file in your child theme then create a blank file with the same name).

We will use font awesome icons here, the simplest way to use font awesome in WordPress is by installing Better Font Awesome plugin.

if ( ! function_exists( 'twentyseventeen_posted_on' ) ) :

/**

* Prints HTML with meta information for the current post-date/time and author.

*/

function twentyseventeen_posted_on() {

// Get the author name; wrap it in a link.

$byline = sprintf(

/* translators: %s: post author */

__( 'by %s', 'twentyseventeen' ),

'<span class="author vcard"><a class="url fn n" href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '"><i class="fa fa-user" aria-hidden="true"></i>' . get_the_author() . '</a></span>'

);

// Finally, let's write all of this to the page.

echo '<span class="posted-on">' . twentyseventeen_time_link() . '</span><span class="byline"> ' . $byline . '</span>';

}

endif;

Now you can see a user icon beside the author name.

A tip: you can make these changes directly in the content.php file but it is a good practice to follow the theme structure.

Adding Post Meta Data

So far we have customized existing meta tags. Now to spruce things up – let’s assume we want to display category and tags after the post title.

To do this, copy the content.php file from the parent theme and paste it in your child theme, keeping the same directory pattern.

twentyseventeen > template-parts > post  > content.php to twentyseventeen-child > template-parts > post > content.php

Now you have to write a new function.

Think it’s tough? Actually it’s not, here’s what you need to do.

Most themes will show category and tags somewhere in the post, you just need to locate the file and replicate the code in your function that’s it.

In our instance, in TwentySeventeen theme category and tags are placed after the content. Now we need to follow our previous workflow.

Open content.php and see which function is used for category and tags.

<?php

if ( is_single() ) {

twentyseventeen_entry_footer();

}

?>

Here it is twentyseventeen_entry_footer().

Now go the template-tags.php file and search for twentyseventeen_entry_footer().

if ( ! function_exists( 'twentyseventeen_entry_footer' ) ) :

/**

* Prints HTML with meta information for the categories, tags and comments.

*/

function twentyseventeen_entry_footer() {

 

/* translators: used between list items, there is a space after the comma */

$separate_meta = __( ', ', 'twentyseventeen' );

 

// Get Categories for posts.

$categories_list = get_the_category_list( $separate_meta );

 

// Get Tags for posts.

$tags_list = get_the_tag_list( '', $separate_meta );

 

// We don't want to output .entry-footer if it will be empty, so make sure its not.

if ( ( ( twentyseventeen_categorized_blog() && $categories_list ) || $tags_list ) || get_edit_post_link() ) {

 

echo '<footer class="entry-footer">';

 

if ( 'post' === get_post_type() ) {

if ( ( $categories_list && twentyseventeen_categorized_blog() ) || $tags_list ) {

echo '<span class="cat-tags-links">';

 

// Make sure there's more than one category before displaying.

if ( $categories_list && twentyseventeen_categorized_blog() ) {

echo '<span class="cat-links">' . twentyseventeen_get_svg( array( 'icon' => 'folder-open' ) ) . '<span class="screen-reader-text">' . __( 'Categories', 'twentyseventeen' ) . '</span>' . $categories_list . '</span>';

}

 

if ( $tags_list ) {

echo '<span class="tags-links">' . twentyseventeen_get_svg( array( 'icon' => 'hashtag' ) ) . '<span class="screen-reader-text">' . __( 'Tags', 'twentyseventeen' ) . '</span>' . $tags_list . '</span>';

}

 

echo '</span>';

}

}

 

twentyseventeen_edit_link();

 

echo '</footer> <!-- .entry-footer -->';

}

}

endif;

Find the code for category and tags then copy and paste the code in your child theme's function.php file within a new function, like this -

if ( ! function_exists( 'twentyseventeen_post_meta' ) ) :

/**

* Prints HTML with meta information for the current post-date/time and author.

*/

function twentyseventeen_post_meta() {

/* translators: used between list items, there is a space after the comma */

$separate_meta = __( ', ', 'twentyseventeen' );

// Get Categories for posts.

$categories_list = get_the_category_list( $separate_meta );

// Get Tags for posts.

$tags_list = get_the_tag_list( '', $separate_meta );

echo '<span class="cat-tags-links">';

// Make sure there's more than one category before displaying.

if ( $categories_list && twentyseventeen_categorized_blog() ) {

echo '<span class="cat-links"><i class="fa fa-folder-open" aria-hidden="true"></i><span class="screen-reader-text">' . __( 'Categories', 'twentyseventeen' ) . '</span>' . $categories_list . '</span>';

}

if ( $tags_list ) {

echo '<span class="tags-links"><i class="fa fa-tags" aria-hidden="true"></i><span class="screen-reader-text">' . __( 'Tags', 'twentyseventeen' ) . '</span>' . $tags_list . '</span>';

}

echo '</span>';

};

endif;

You can see I have used font awesome icons for both categories and tags.

Styling your Post Meta

Since we have placed our meta tags in our desired location, it’s time to adjust some style.

It’s very straightforward if you know basics of  CSS. Let’s adjust our category and tags section.

.entry-header .cat-tags-links {

font-size: 14px;

}

.entry-header .cat-links {

padding-right: 5px;

}

Adjusted category and tags section

Now it looks different.

But if you want to show your own custom icon instead of font awesome or any other, then you can use your image as background image in CSS like this:

.entry-header .cat-links {

background: url(category-img.jpg) left center no-repeat;

padding-left: 5px;

padding-right: 5px;

}

.entry-header .tags-links {

background: url(tag-img.jpg) left center no-repeat;

padding-left: 5px;

padding-right: 5px;

}

Once done don’t forget to remove font awesome code from your function.php file.

Remove Any Post Meta

Removing any post meta is very simple, just find the code and delete it.

Meta tag duplication

For example, if you see our single post page now the categories and tags are now appearing in two sections. If you don’t want to display it after the content, then simply delete the category and the tag code from twentyseventeen_entry_footer() function.

So the new code would look like this in your child theme.

if ( ! function_exists( 'twentyseventeen_entry_footer' ) ) :

/**

* Prints HTML with meta information for the categories, tags and comments.

*/

function twentyseventeen_entry_footer() {

// We don't want to output .entry-footer if it will be empty, so make sure its not.

if ( get_edit_post_link() ) {

echo '<footer class="entry-footer">';

twentyseventeen_edit_link();

echo '</footer> <!-- .entry-footer -->';

}

}

endif;

Conclusion

As you can see post meta data can be displayed in many ways, but it’s best if you acquire a basic understanding of your theme structure first and only then if possible make adjustment accordingly. This minimizes the possibility of your theme breaking down. P.S. don’t forget to create a backup.

I hope this post has helped you to understand how post meta data works, but if you have any relevant questions do post your comments below.

获取当前user id和其他user相关信息

1. Find User ID in WordPress Admin Area

This is a very simple method, but it also have some disadvantages. First – you should be logged in, second – you can not obtain the id of your own user.

So:

  1. Log into your WordPress admin
  2. Go to Users > All usersAll users link in the menu
  3. Choose the user and go to his profile
  4. Look at the URL of the page:

2. Get Current User ID (and username, email etc)

The best way to get a currently logged in user ID is using get_current_user_id() function.

$current_user_id = get_current_user_id();

Absolutely the same way is with wp_get_current_user():

$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

The usage of get_current_user_id() seems simpler for me but you can use any way you want, because in the code they are the same. But wp_get_current_user() allows you at the same time to get current user email $current_user->user_email, first name $current_user->first_name, last name $current_user->last_name, username $current_user->user_login and display name $current_user->display_name.

But did you hear about get_currentuserinfo()? Should you use it? The answer is – no, this function is deprecated. Use wp_get_current_user() instead.

3. How to Get User ID by Email?

Simple enough. Just use get_user_by() function.

$the_user = get_user_by('email', 'misha@rudrastyh.com');
$the_user_id = $the_user->ID;

Bonus: Get User Email by ID

First of all you can get user email with the same get_user_by() function.

$the_user = get_user_by( 'id', 54 ); // 54 is a user ID
echo $the_user->user_email;

The next method is just get_userdata() function, which is equal to get_user_by( 'id', ... )

$the_user = get_userdata( 54 );
echo $the_user->user_email;

4. Get User ID by Username (login name)

In the following example the second parameter “rudrastyh” is the username.

$the_user = get_user_by('login', 'rudrastyh');
$the_user_id = $the_user->ID;

Is there a way to get username from user ID? Yes, just use get_userdata() or get_user_by( 'id', ... ). Just like the example with emails.

5. Get User ID by First Name or by Last Name

Print the ids of all users with the first name is “Misha”:

global $wpdb;
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Misha'" );
if( $users ) {
	foreach ( $users as $user ) {
		echo '<p>' . $user->user_id . '</p>';
	}
} else {
	echo 'There are no users with the specified first name.';
}

Print the ids of all users with the last name is “Rudrastyh”:

global $wpdb;
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Rudrastyh'" );
if( $users ) {
	foreach ( $users as $user ) {
		echo '<p>' . $user->user_id . '</p>';
	}
} else {
	echo 'There are no users with the specified last name.';
}

The code above will work if you would like to find user ID by any user meta value. Just replace meta_key and meta_value with the ones you need.

And of course you can get user first and last names and any meta with the help of get_user_meta( $id, $meta_key, true) function.

6. How to Get Author ID by Post ID?

In this case you can get the user ID from WP_Post object. It’s simple.

$my_post = get_post( $id ); // $id - Post ID
echo $my_post->post_author; // print post author ID

7. How to Get a Customer ID from an Order in WooCommerce?

There are two different ways to do it, the first one is just to get customer ID from order meta this way:

$customer_id = get_post_meta( 541, '_customer_user', true); // 541 is your order ID

The second one is with the help of WC_Order object. By the way this method will work only for WC 3.0+.

$order = wc_get_order( 541 ); // 541 is your order ID
$customer_id = $order->get_customer_id(); // or $order->get_user_id() – the same

8. Add the User ID column to the WordPress Users Table

Very useful way of getting user ids through WordPress admin. Here is the result:

User ID column in the users table

How to create the same column on you own WordPress website? All you need to do is to copy the following code to you current theme functions.php file:

/*
 * Adding the column
 */
function rd_user_id_column( $columns ) {
	$columns['user_id'] = 'ID';
	return $columns;
}
add_filter('manage_users_columns', 'rd_user_id_column');
 
/*
 * Column content
 */
function rd_user_id_column_content($value, $column_name, $user_id) {
	if ( 'user_id' == $column_name )
		return $user_id;
	return $value;
}
add_action('manage_users_custom_column',  'rd_user_id_column_content', 10, 3);
 
/*
 * Column style (you can skip this if you want)
 */
function rd_user_id_column_style(){
	echo '<style>.column-user_id{width: 5%}</style>';
}
add_action('admin_head-users.php',  'rd_user_id_column_style');

如果要获取到当前用户的meta信息,就用到get_user_meta( int $user_id, string $key = ”, bool $single = false )。