0

Here is my WP_Query in which I want to fetch data from multiple custom post types and order them by the modified date:

$args = array(
  'post_type'      => array( 'staff-updates', 'employees', 'coreteam', 'developing-updates', 'vacancies', 'recognition', 'page' ),
  'orderby'        => 'modified',
  'posts_per_page' => '10',
  'order'          => 'DESC',
);
$the_query = new WP_Query( $args );

In this I have an exception to fetch only to posts published for custom post type employees and not modified in the same query.

  • 1
    Your question is confusing, please file an edit and explain properly what your issue is – Pieter Goosen Dec 28 '15 at 10:45
  • The issue is I have to write a wp_query in which I have to fetch data from different Custom Post Types. The post types are mentioned in the quetion. However, as per the requirement, the modified post has be to retrieved i.e. orderby => modified Now the issue is; except post type employees the above condition is valid while for post type employees it has to orderby => date – Aniket Bhawkar Dec 29 '15 at 19:45
0

Am Afraid that you have to go with Direct MYSQL or you can run two WP_Query() and then combine them, sort them or sort them with WP_Query() then combine them.

Here is an example

$args = array(
  'post_type'      => array( 'staff-updates', 'coreteam',
                           'developing-updates', 'vacancies', 'recognition', 'page' ),
  'orderby'        => 'modified',
  'posts_per_page' => '10',
  'order'          => 'DESC',
);
$the_query_modified = new WP_Query( $args );

$args = array(
  'post_type'      => 'employees',
  'orderby'        => 'date', //default to post_date
  'posts_per_page' => '10',
  'order'          => 'DESC',
);
$the_query_published = new WP_Query( $args );

$combined = new WP_Query();
//combining two to one
$combined->posts = array_merge( $the_query_modified->posts, $the_query_published->posts );

Now $combined->posts will have both. Then do whatever you want. $combined might miss few details since it is not direct constructed. In that case you just manipuldate with two queryies( $the_query_modified,$the_query_published ) directly or go for SQL

  • Thanks for the quick solution. It works partially! I figured out a patch over this to sort the posts by date_modified (rest all post types) and date_published (employees) and display the latest. – Aniket Bhawkar Dec 29 '15 at 19:51
  • Ok gald it helped you. Is your patch running single Query or Two Query? – WPTC-Troop Dec 30 '15 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.