46627

How do I execute a WP_Query with multiple arguments?

Question:

I am trying to do a WP_Query where I want the loop to execute only if the post type is books and genre is the text in $genre. I keep getting an error as it displays all the posts in the books post type instead of the particular genre required.

I have tried this:

<?php $genre ="suspense"; $args = array('post_type' => 'books','genre' => $genre); //Define the loop based on arguments $loop = new WP_Query( $args ); //Display the contents while ( $loop->have_posts() ) : $loop->the_post(); ?>

Answer1:

You need to compare the value of the custom field and perform a meta_query. Try the following code:

<?php $genre ="suspense"; $args = array( 'post_type' => 'books', 'meta_query' => array( array( 'key' => 'genre', 'value' => $genre, 'compare' => '=' ) ) ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); ?>

Read more on the WordPress Codex here: <a href="http://codex.wordpress.org/Class_Reference/WP_Query" rel="nofollow">http://codex.wordpress.org/Class_Reference/WP_Query</a>.

Recommend

  • Search for value in multidimensional array and get parent array in PHP
  • SQL query to include specific WooCommerce tag
  • PHP: How to put a variable in an array?
  • Wordpress - How to change search value using a drop down selector?
  • Dispose not working as expected in DirectorySearcher
  • Fluid video height
  • Weird LEFT OUTER JOIN on Includes eager loading of rails 3
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • Does Mobilefirst provide a provision to access web services directly?
  • Webgrid not refreshing after delete MVC
  • How to Cache Real-time Data?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Initializer list vs. initialization method
  • When to use `image` and when to use `Matrix` in Emgu CV?
  • Spring Data JPA custom method causing PropertyReferenceException
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • What is the “return” in scheme?
  • Date difference with leap year
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • A cron job substitute?
  • json Serialization in asp
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Does armcc optimizes non-volatile variables with -O0?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal