mardi 3 février 2015

Wordpress function to query by custom field and category


A function was written for our small non-profit org by a developer who is no longer available. The function works well, and we now need a similar function as a modification of the query criteria.


This function returns a list of links that are filtered by two variables




  1. Filtered by the author of the current page – so that the links provided are only pages authored by the same author.




  2. The second filter is by category




The result is a list of links to pages within the category which were authored by the same author.



function team_resources_posts() {
global $authordata, $post;

$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'category_name' => 'team-resources' ) );

$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>';
}
$output .= '</ul>';

return $output;


}


We need a similar function that filters by custom field key instead of Author. The second criteria – “category” would remain unchanged.


Example: If the key on the current page for the custom field “Project ID” is “Project 1″ THe function will return a list of pages which



  1. Have key “Project1″

  2. Are associated with the category as in the original script.


Please write if I need to explain the need further. Please let us know estimated time and cost to provide the code. I can provide a logon to our site with a sample page ready to test.


Thanks, wh





Aucun commentaire:

Enregistrer un commentaire