• Resolved badi2018

    (@badi2018)


    Hi,
    I’m using your awesome plugin to store my data into a custom table only (not in postmeta) and I need to do custom search using wp_query;
    here’s my code :

    $args = array(
        'post_type' => 'event',
        'post_status' => array('publish'),
        'meta_key'=>'location',
        'meta_value'=>'de'
    );
    
    $posts = new WP_Query($args);

    but the results seems to be empty, I assume it’s because I’m not storing data into the postmeta table.

    is there a way (other than wpdb…) to overcome this case ?

    thanks a lot for your help

Viewing 1 replies (of 1 total)
  • Plugin Author Abhishek

    (@abhisheksatre)

    Hi,

    Using WP_Query on a custom table isn’t supported.

    To work around this limitation, you can follow these steps:

    1. Write a SQL query that selects all post IDs that meet your condition
    2. Pass an array of post IDs to the post__in WP_Query argument to query the posts.
    <?php
    
    // Retrieve post IDs where the location is 'de'
    global $wpdb;
    $query = "select post_id from WP_CUSTOM_TABLE where location=%s";
    $postIdQuery = $wpdb->prepare($query, 'de');
    $postIds = $wpdb->get_col( $postIdQuery );
    
    // Retrieve posts whose IDs are contained in the $postIds array.
    if (is_array($postIds) && count($postIds) > 0) {
    	$query = new WP_Query( [
    		'post_type' => 'event',
        	'post_status' => array('publish'),
    		'post__in' => $postIds,
    	] );
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Need to do a custom search by meta key’ is closed to new replies.