0

I add meta box in mobile CPT, Where I add a post with Ajax successfully. Here is code

function metabox_for_select2() {
add_meta_box( 'rudr_select2', 'My metabox', 'display_select2_metabox', 'mobile', 'normal', 'default' );
}

function display_select2_metabox( $post_object ) {
$html = '';
$appended_posts = get_post_meta( $post_object->ID, 'select2_posts',true );
$html .= '<p><label for="select2_posts">Posts:</label><br /><select id="select2_posts" name="select2_posts[]" multiple="multiple">';

if( $appended_posts ) {
    foreach( $appended_posts as $post_id ) {
        $title = get_the_title( $post_id );
        $title = ( mb_strlen( $title ) > 50 ) ? mb_substr( $title, 0, 49 ) . '...' : $title;
        $html .=  '<option value="' . $post_id . '" selected="selected">' . $title . '</option>';
    }
}
$html .= '</select></p>';

echo $html;
}

Now after adding posts to meta box when I open a post, I need to show all those posts, where meta box have this post.

This query shows all post which I added to meta box on different posts not the specific. How to manage query after that its show recommended result.

<?php   
    $posts = get_post_meta( $post_id, 'select2_posts', true );
    $args = array(
     'posts_per_page' => 10,
     'post_type' => 'mobile',
     'post__in' => $posts
 );

$results = new WP_Query($args);
while ($results->have_posts()) {
    $results->the_post();
    the_title();
}
wp_reset_postdata();           
?>  

How to get those posts where meta box have this post or link?

I am using this query on single.php and trying to show all post here where meta box has this post.

Example:

Add 2 Custom Post Type = mobile & processor

processor have 2 posts = Nvidia , Intel

mobile have 2 posts = Apple , Nokia

Add a meta box select2_posts to CPT mobile

After that, Apple using Nvidia processor & Nokia using Intel processor so add processor to meta box.

Finaly Here Attached nvidia to Apple & intel to Nokia inside meta box

Now i want when i open Nvidia post in sidebar Apple mobile show & when i open Intel post in sidebar Nokia mobile show.

In simple word show those mobile which use this processor which i currently open.

1 Answer 1

0

From what i understand of your question, you are trying to do a meta query. You can do this by the following arguments:

$args = array(
    'posts_per_page' => 10, 
    'post_type' => 'mobile',
    'meta_query' => array(
        array(
            'key' => 'select2_posts',
            'compare' => 'LIKE',
            'value' =>  $value_here
        )
    )
);

You can read more about meta queries here.

3
  • Problem with your query it get all the post. But i need only those post where open/current post added
    – FRQ6692
    Jun 1, 2017 at 6:48
  • @Farooq Hi. Unfortunately i can't understand your question well... I've read your question a few times. Can you add an example to your question so i can understand it?
    – Johansson
    Jun 1, 2017 at 16:58
  • please check example. I attached it to the question
    – FRQ6692
    Jun 1, 2017 at 17:56

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.