Mysql query for all posts in specific category


SELECT * FROM wp_posts p
LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND t.slug = 'category-slug'
Advertisements

Yoast breadcrumbs no separator

I was working on a project where I wanted to use WordPress Yoast SEO plugin breadcrumbs, but don’t wanted to use a string separator. I wanted to style <span>’s in the breadcrumb with a right CSS border radius.

I wrote this hook to remove the seperator:

// Filter hook Yoast seo breadcrumbs no seperator
function no_separator( $output ) {
 $replace = array( '&raquo;' );
 $output = str_replace( $replace, '', $output );
 return $output;
}
add_filter('wpseo_breadcrumb_single_link_with_sep', 'no_separator');

 

 

WordPress blog url without http

If you want to show your WordPress URL without http, you can do it by a str_replace function in combination with the WordPress get_bloginfo(‘wpurl’) function, as follows:

Add al the protocols that you want to remove in an array, and then str_replace the get_bloginfo(‘wpurl’) function.

<?php
 $protocols = array('http://', 'http://www.', 'www.');
 echo str_replace($protocols, '', get_bloginfo('wpurl'));
 ?>

That’s it!

WordPress disable dashboard for non-admin

By adding this function to your functions.php, non-admin users can’t approach the WordPress admin / dashboard.

// No WordPress admin for non-administrators
add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {
 if (!current_user_can('manage_options') && $_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {
 wp_redirect(home_url()); exit;
 }
}

I have used this in a project where I created a complete dashboard in the frontend of the website, so where no non-admin user was allowed to visit the WordPress admin / dashboard.

 

WordPress show admin bar only for Admin

In some situations it is helpful to don’t show the WordPress admin bar to all the user types. By adding the following code to your function.php, the admin bar only shows up when your logged in as Administrator.

// show admin bar only for admins
if (!current_user_can('manage_options')) {
 add_filter('show_admin_bar', '__return_false');
}

Related posts by author

For displaying related posts by author somewhere in your WordPress website you can use the following function:

function get_related_author_posts() {
 global $authordata, $post;

 $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) );

 if( $authors_posts && count( $authors_posts ) >= 1 ) :
 $output = '<ul>';
 foreach ( $authors_posts as $authors_post ) {
 $output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a>' . apply_filters( 'the_excerpt', $authors_post->post_excerpt, $authors_post->ID ) . '</li>';
 }
 $output .= '</ul>';
 else :
 $output = '<div class="error"><p>'. __( 'Nog geen artikelen geschreven...' ) .'</p></div>';
 endif;
 return $output;

 } 

 /*add this hook where you need the related posts*/
 echo get_related_author_posts();
 ?>

Tip! If you want to use posts from a custom post type, you need to specify this in get_posts() by adding: ‘post_type’ => ‘post-type-name’.

WordPress search count results

You want to create a search page in WordPress where you count the results and show them on your page, for example: Searching for “apple” gives 3 results.

For getting this search counting you need to add the following code in your search file where you want to show this “counting sentence”.

First we make these variables:

<?php
$allsearch = &new WP_Query("s=$s&showposts=-1");
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count;
?>

Then we use $key for showing the search query and $count for how much results.

<p>Searching for "<?php echo $key;?>" gives <?php echo $count;?> results.</p>

How to add an extra field to comment.php

Today I’ve searched the whole internet for how to add an extra field to the comments.php file which is visible in a comment in my admin. I have a comments.php file with a name, e-mail and textfield. But I want to add an extra field for location.

There are lots of tutorials for adjusting the default WordPress comment_form but I am searching for a piece of code for a custom made comment form.

If you can give me a step in the right direction, let me know in the comments please.