Set a cookie only in single posts

init is too early (it happens before the main query), use the action template_redirect instead. And for the ID use get_the_ID(). Example: if ( is_singular() ) setcookie( get_the_ID(), ‘visited’,time() + ( DAY_IN_SECONDS * 31 ) );

Single.php different behaviour from admin to non-admin

Per a comment, the problem was due to the use of query_posts which clobbers the main query. Please don’t use query_posts. It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. … Read more

Including all post id’s of a custom post type into an array

Save yourself a loop with get_posts and ‘fields’ => ‘ids’. // Default Query function do_query($args = null) { $default_args = array( ‘numberposts’ => -1, ‘posts_per_page’ => -1, ‘post_status’ => ‘publish’, ‘fields’ => ‘ids’, ); if (null !== $args && !empty($args)) $args = array_merge($default_args, $args); else $args = $default_args; return get_posts($args); } // Customize Query $post_ids … Read more

Which is better and faster ? WordPress Queries or SQL Query

Queries ofcourse. It’s faster… But in this case just please delete you wp site and start with something faster… here is my superfast framework for you… <?php /*Your bunny wrote */ I did tests, 0.0000001 runtime vs WP usually 0.7-2.8 Sarcasm off P/S/ This question have no sence since using direct queries and output of … Read more

Get term name without a foreach loop

What about using: echo get_the_term_list( $post->ID, ‘artist-genre’, ‘<li>’, ‘,</li><li>’, ‘</li>’ ); instead, to generate the HTML list? Or simply: the_terms( $post->ID, ‘artist-genre’, ‘<li>’, ‘,</li><li>’, ‘</li>’ ); that’s a wrapper for get_the_term_list(). Also notice that you’re missing the is_wp_error() check, in your code snippet, because wp_get_post_terms() can return the WP_Error object, for an unknown taxonomy. But … Read more

Cannot retieve the_content() and the_author() – both returning empty strings

get_the_content and the_author have to be in a loop so you would need to use other functions to get the content you need global $wp_query; $post = $wp_query->post; $page_id = $post->ID; // page ID $page_object = get_page( $page_id ); // page stuff $author_id = $post->post_author; // author ID $page_content = $page_object->post_content; $author_name = get_the_author_meta( ‘display_name’, … Read more