How to sort queried pages by an array of page ids?

There is no need to use such custom code when wordpress provides a better way of ordering the pages using menu_order. You can see this option under Page Attributes box on the right side of page edit screen. You can assign the ordering as per your criteria, then use the following code <?php global $wp_query; … Read more

WordPress Queries Not Working?

For the sidebar bit, you should be using a new Wp_Query: if ( $query->is_category(‘movies’) ): $movies = new Wp_Query(‘tag=movie-reviews’); while ( $movies->have_posts() ) : $movies->the_post(); get_template_part( ‘sidebar-reviews’, get_post_format() ); endwhile; else: get_template_part( ‘no-results’, ‘index’ ); endif; You don’t need query filters for anything except the main loop on a page.

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

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);