Can I make a search query which includes a space?

If it is possible at all, this is how it would be done: $searchquery = new WP_Query( array( ‘s’ => ‘hello ‘ )); Since all queries boil down to WP_Query at their most basic level, we can determine if it is possible using the WordPress APIs by looking at the code in wp-includes/query.php // If … Read more

How to add a recent post function to a text widget I created for practice?

In the function widget( $args, $instance ), which generates the widget output, add the following code before the echo $after_widget; line: // The Query $the_query = new WP_Query( array( ‘post_type’ => ‘page’, ‘posts_per_page’ => ‘3’ ) ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo ‘<li>’ . get_the_title() . ‘</li>’; endwhile; Additional information … Read more

How to WP Query custom multiple custom taxonomies?

Here is the way I think it should work: $args = array( ‘post_type’ => ‘item’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘tax_query’ => array( array( ‘taxonomy’ => ‘color-categories’, ‘field’ => ‘slug’, ‘terms’ => array(‘red’, ‘2000’), ‘operator’ => ‘AND’ ) ) ); So you query posts which are in both red and 2000 using operator AND.

Change post query onclick

Yes, if you need to run WordPress/PHP functions from a button click (Javascript), you need Ajax. In the following example, a shortcode will render a button (id=”newquote”) that calls an Ajax action hook (get_random_cpt). A query is done and replaces one div from that shortcode (id=”randomquotes”) with the results. How to Use Ajax in a … Read more

WordPress SQL JOIN query

If you are going to do this is SQL, use a subquery. SELECT *, (SELECT meta_value FROM wp_commentmeta WHERE meta_key = ‘your-meta-key’ AND wp_commentmeta.comment_id = wp_comments.comment_ID LIMIT 1) as comment_author FROM wp_comments Instead of the *, enumerate the fields you want but leave out comment_author. Obviously, $wpdb functions to keep the table names straight.

Loop for custom-post-type comparing taxonomy terms for “related” posts?

Change your $args array a little bit: $args = array( ‘post_type’ => array( ‘my_projects’ ), // it need to get the posts from this post_type ‘order’ => ‘DESC’, ‘posts_per_page’ => 5, ‘project_tags’ => $term // search taxonomy ); Other ways to search for taxonomy terms can be found here in the WordPress Codex pages. Answer … Read more

Creating ‘posts page’ loop based on the page itself

You can get the selected page for posts by the following code: $page_for_posts = get_option( ‘page_for_posts’ ); Then, get the requested post by: $post = get_post( $page_for_posts ); And, get the data you need: echo apply_filters( ‘the_title’, $post->post_title ); echo apply_filters( ‘the_content’, $post->post_content ); echo get_post_meta( $post->ID, $key, $single);