Get the ID after WP_Query

If you need to get the ID from the current page, you should make use of get_queried_object_id() which will return the ID of the current queried object. A new instance of WP_Query alters the $post global variable, that is why after your loop you’ll see the value of the last post from your custom query … Read more

Display only post types that are direct children of the current taxonomy

Did you try WP_Query? Using my knowledge and the WP_Query documentation, I did this: $args = array( ‘post_type’ => ‘my_post_type’, //change the post type here ‘post_status’ => ‘publish’, ‘tax_query’ => array( array( ‘taxonomy’ => ‘my_category’, //change the taxonomy name here ‘field’ => ‘id’, ‘include_children’ => false, ‘terms’ => 10 //change the term id here ) … Read more

Adding class to featured image

You can add custom class to post thumbnails. the_post_thumbnail accepts array of attribute $attr where you can specify image class. So your code will be. <div class=”home-featured-img”> <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail( ‘full’, array( ‘class’ => ‘responsive-class’ ) ); // show … Read more

Set object terms not working on plugin activation

register_activation_hook is one-time only activation hook. It can be used for database structure creation, as example, that needs to fire only on plugin activation trigger. In order to set a custom taxonomy you need a hook that will fire not just once, but per every WordPress load. Such as init. Use add_action( ‘init’, ‘YOUR_CALLBACK’ ); … Read more

Set a default category – if the user didn’t select one- before creating a post

Maybe this is what you are looking for? Source is from @Michael Fields /** * Set default cat for cpt * @source {https://circlecube.com/says/2013/01/set-default-terms-for-your-custom-taxonomy-default/} * @source {http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/} * @license GPLv2 */ function set_default_object_terms_203962( $post_id, $post ) { if ( ‘publish’ === $post->post_status ) { $defaults = array( //’your_taxonomy_id’ => array( ‘your_term_slug’, ‘your_term_slug’ ) ‘post_tag’ => array( … Read more

How to check if “is single” page

You can use is_singular() with post types like this: if( is_singular(‘event’) ) { // We are in single view of event post type } So, your code could be: add_filter( ‘the_title’, ‘event_page_title’ ); function event_page_title( $title ) { if ( is_singular( ‘event’ ) ) { $title = “Title: ” . $title; } return $title; }

Editable content on a Custom Archive page

You can use Options API or Setting API to store the data in the database. Write a plugin to create a meta box for the introduction text and show that text on the archive-unicorn.php. Or you can utilize the description parameter when you register_post_type and print it in the archive-unicorn.php like this: $unicorn_obj = get_post_type_object(‘unicorn’); … Read more