how to check custom post type in wordpress

wordpress tutorial, how to check custom post type in wordpress. Many we want to execute some code on custom post type or want to execute code on post. Many we want to execute some code on custom post type or some times we want to execute code on normal pages or post.

how to check custom post type in wordpress

So here is solution. You just need to just add following code into your functions.php file which you can find in your wordpress theme folder.

function check_custom_post_type() {
global $wp_query;

$post_types = get_post_types(array('public'   => true,'_builtin' => false),'names','and');

foreach ($post_types  as $post_type ) {
if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) {
return true;
} else {
return false;
}
}
}

After this you can use following function in any wordpress file. For example you can use this method in single.php file.

if (check_custom_post_type()) {
//Current post is a custom post type
}
how to check custom post type in wordpress
wordpress tutorial, how to check custom post type in wordpress. Many we want to execute some code on custom post type or want to execute code on post.

How can we save Ram usages using some wordpress theme tricks

While running wordpress site, save Ram usage is always great idea and you can easily improve the site performance by simple wordpress theme tricks.

We mostly use the get_permalink(), get_the_title() methods in our wordpress theme. Do not pass the post ID as parameter.

if you’re doing a get_permalink or get_title() call with Post id, 8 out of 10 times you’ll need to more of that post than just the permalink, so this isn’t really a problem.

save Ram usage

Post object is actually already slightly faster than calling get_permalink with $post->ID (in get_post it then only sanitizes and adds to cache, it doesn’t fetch new data), but the real benefit comes when you add a variable called filter in the $post object, setting it to “sample”. Now you decide whether that post object is going to be cached or not and which variables it contains.

Pass the $Post object instead of Post ID.

Do not use the Custom fields. Your server need to fire extra custom quries on Mysql server.

If your are using $wpdb->get_results or new WP_Query( $args ) then add the order by.

How can we save Ram usages using some wordpress theme tricks
How can we save Ram usages using some wordpress theme tricks

wordpress WP_Query vs query_posts vs get_posts

WordPress tutorial, wordpress WP_Query vs query_posts vs get_posts. There are three ways for fetching data(posts) from wordpress. query_posts  more often than others. But I prefer to use WP_Query(). WP_Query is object base more secure then others.

wordpress WP_Query vs query_posts vs get_posts

wordpress WP_Query vs query_posts vs get_posts
wordpress WP_Query vs query_posts vs get_posts

query_posts() is overly simplistic and problematic way to to modify main query of page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with pagination). Any modern WP code should use more reliable methods, like making use ofpre_get_posts hook, for this purpose. TL;DR don’t use query_posts() ever;

get_posts() is very similar in usage and accepts same arguments (with some nuances, like different defaults), but returns array of posts, doesn’t modify global variables and is safe to use anywhere;

WP_Query class powers both behind the scenes, but you can also create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere.

The basic difference is that query_posts() is really only for modifying the current Loop. Once you’re done it’s necessary to reset the loop and send it on its merry way. This method is also a little easier to understand, simply because your “query” is basically a URL string that you pass to the function, like so:

query_posts(‘meta_key=color&meta_value=blue’);
On the other hand, wp_query is more of a general purpose tool, and is more like directly writing MySQL queries than query_posts() is. You can also use it anywhere (not just in the Loop) and it doesn’t interfere with any currently running post queries.