Horje
Custom SQL Query? - Wordpress Solution
I am trying to splice together 2 post types and pull in 8 posts/events greater than the current day. I am struggling to get the default post type posts to show up though. Right now only the events are showing up - and I think it is something to do with my meta_query. The code is below:
global $post;
$today = date( 'Ymd' );
$args = array(
   'post_type' => array('post', 'tribe_events'),
   'post_status' => 'publish',
   'posts_per_page' => 8,
   'meta_query' => array(
      'relation' => 'OR',
      'event_date' => array(
        'key' => '_EventStartDate',
        'compare'   => '>=',
        'value'     => $today,
      ),
      'date' => array(
        'key' => 'date',
        'compare'   => '>=',
        'value'     => $today,
      )
   ),
   'orderby' => array( 'event_date' => 'ASC', 'date' => 'ASC' ),
);
Any help would be greatly appreciated.

Solution - 1

Hello the problem is that post dont have the meta. maybe adding one more condition to the meta query will help like this global $post; $today = date( 'Ymd' ); $args = array( 'post_type' => array('post', 'tribe_events'), 'post_status' => 'publish', 'posts_per_page' => 8, 'meta_query' => array( 'relation' => 'OR', 'event_date' => array( 'key' => '_EventStartDate', 'compare' => '>=', 'value' => $today, ), 'date' => array( 'key' => 'date', 'compare' => '>=', 'value' => $today, ), array( 'key' => '_EventStartDate', 'value' => '', 'compare' => 'NOT EXISTS', ) ), 'orderby' => array( 'event_date' => 'ASC', 'date' => 'ASC' ), ); but the best solution is have the 2 posts types have the same meta field name to compare


Solution - 2

Try this custom query:

$list = $wpdb->get_col("
	select p.id
	from
		{$wpdb->prefix}postmeta pm
		left join {$wpdb->prefix}posts p on p.id = pm.post_id
	where
		(
			(pm.meta_key='_EventStartDate' and p.post_type='tribe_events')
			OR
			(pm.meta_key='date' and p.post_type='post)
		)
		AND
		p.post_status = 'publish'
		AND
		pm.meta_value >= '$today'
	order by pm.meta_value ASC limit 8");
$list will contain id of posts in true order. Then use get_post() for each.


Solution - 3

Hi James, Here you are the code


<?php
/******************************************************
* code by : Mohamed Ahmed (#Eldigital)
* <a rel="nofollow" href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b1b3bebebfb7bbbca1a6b3bca6beab92b5bfb3bbbe">[email protected]</a>
* Splice together 2 post types and pull in 8 posts/events greater than the current day.
* Struggling to get the default post type posts to show up though.
* Right now only the events are showing up.
*/

global $wpdb;

$sql_query = "
	SELECT DISTINCT $wpdb->posts.*
	FROM $wpdb->posts, $wpdb->postmeta
	WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
	AND
	(
		$wpdb->posts.post_status = 'publish'
		OR
		$wpdb->posts.post_status = 'future'
	)
	AND
	(
		(
			$wpdb->posts.post_type = 'post'
			AND
			$wpdb->posts.post_date >= NOW()
		)
		OR
		(
			$wpdb->posts.post_type = 'tribe_events'
			AND
			$wpdb->postmeta.meta_key = '_EventStartDate'
			AND
			$wpdb->postmeta.meta_value >= NOW()
		)
	)
	ORDER BY $wpdb->posts.post_date ASC LIMIT 8
	";

$posts = $wpdb->get_results($sql_query);

As this is WordPress SQL query, wordpress modify and results the answer equivalent to INNER JOIN. .


Solution - 4

hello, can you try using this code instead?

$get_events = tribe_get_events(array(
    'post_type' => array('post', 'tribe_events'),
    'post_status' => 'publish',
    'start_date' => tribe_event_beginning_of_day(date('j M Y')),
    'posts_per_page' => 8
), true);


Solution - 5

'post_type' => array('tribe_events', 'post')
try changing your post_type


Solution - 6

You are doing a meta_query using _EventStartDate but posts do not have that meta field so it will return negative for any of those. It isn't possible. You would have to add a new meta field for the posts to recognize in your OR part of the query.





Wordpress

Related
Plugin User Role Level Error - Wordpress Solution Plugin User Role Level Error - Wordpress Solution
Walk array calculate shift - Wordpress Solution Walk array calculate shift - Wordpress Solution
Page content in ACF Flexible Content loop - Wordpress Solution Page content in ACF Flexible Content loop - Wordpress Solution
Gravity forms Facebook pixel advanced matching - Wordpress Solution Gravity forms Facebook pixel advanced matching - Wordpress Solution
Get WPUF Pro to work with future/schedules posts - Wordpress Solution Get WPUF Pro to work with future/schedules posts - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
13