I have a custom post type called activities and a number of standard pages that are different countries. Each country has a number of activities that are available. The user needs to add a range of different activities, and then choose what country each activity is available in.

Using list pages, I have a dropdown listing all pages in the activities custom post type. The user is able add a new activity, add the content and select what country(page) this activity relates to. On that particular country page on the front-end the available activities are listed.

For each country I only want to show 3 activities. I do the standard loop getting all custom post types for activities. I have to check whether the postid of the page chosen in the drop down menu matches the current page, and if it does show the activity. Using the standard posts_per_page isn't working, as it only grabs three posts, then does the conditional statement on those to see if any matches the current id.

I guess what I want to happen is to have the posts_per_page only apply to activities that actually match the criteria of having the country page id matching the id of the country selected in the activity.

global $post;                                                    
$postid = get_the_ID();

$args = array( 'post_type' => 'activities', 'orderby' => 'date', 'order' =>'ASC','posts_per_page' => 3);

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

$country = rwmb_meta( 'rw_activities_pages' ); 
// this is the drop down list of pages. It gets the ID of the country page chosen
$currentID = get_post($country);
$currentTitle = $currentID->post_title;


if ($country == $postid){
// if the activity country id matches the current page show the info
    echo get_the_title();
    echo $currentTitle;
    echo the_content(); 
}   
endwhile;
wp_reset_query();

Any ideas would be fantastic, as I have a couple of custom post types that do a similar thing!

Thanks in advance,

Rich

有帮助吗?

解决方案

What you want to do is filter the posts in the query itself using meta_query.

$args = array( 
  'post_type' => 'activities', 
  'meta_query' => array(
                         'key' => 'rw_activities_page',
                         'value'=> $postid,
                         'compare'=>'='
                         ),
  'orderby' => 'date', 
  'order' =>'ASC',
  'posts_per_page' => 3);

You might need to adjust the meta_key if that's not exactly the value it's using.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top