I have a webpage that I want to display images that are uploaded by a certain author.
In the backend if I look at media, each image has an 'Uploaded By' attribute, and then it says the authors name.
(source: discoveryourwonder.com)
I've tried using this loop:
<?php
// The Query
$args = array(
'author' => $author_name_variable, // Replace with author id
'post_status' => 'any',
'post_type' => 'attachment'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . the_content() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
It is very buggy. Some authors it will show every media file, others none; the rest are just inaccurate. It's a real blind shot :/
The goal is to loop through all the media files, and then post the_content() of all files with the corresponding Uploaded By name.
I would be grateful if someone could comment as to why an ID is more reliable than a slug in the 'author' argument.
I figured out a solution. Apparently the 'author' argument does not like usernames, so I converted it to ID and it works now. I used get_user_by( 'slug', $username ); to get all the information of the particular username, and then assigned that array to a variable. I then filtered the variable to only use the ID and passed that through the arguments.
Here's the working loop:
<?php
// The Query
$profileid = get_user_by( 'slug', $username );
$args = array(
'author' => $profileid->id, // Replace with author id
'post_status' => 'inheret',
'post_type' => 'attachment'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . the_content() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>
Related
Only two categories need to be showed in the homepage. Can anyone help.
You can use WP_Query to get your posts list, and display it with the loop
Example :
$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
In your functions.php file paste the below code:
I am assuming that you want to show categories from two categories which are having ids 5 and 9.
function kiran_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '5,9');
}
}
add_action( 'pre_get_posts', 'kiran_home_category' );
Explanation:
kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
In wordpress WP_query, category__in parameter used to select category with posts.
<?php
$query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
if($query->have_posts()):
echo '<ul>';
while ( $query->have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
?>
For more information about wordpress query click here , you can read more information.
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News') );
$loop = new WP_Query( $args );
if($loop->have_posts()):
?><ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
<h3><?php echo get_the_title();?></h3>
<?php echo $description = get_the_content(); ?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php wp_reset_postdata(); ?>
Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => array('samplecat', 'anothercat'),
'paged' => $paged
);
$arr_posts = new WP_Query($args);
Then do the usual if, while statement..
if($arr_posts->have_posts() ) :
// Start the loop.
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();?>
<?php endwhile;
endif;
I'm building a wordpress site and want to show related posts based on tags when viewing a single post. Currently I'm using the below function to do this and it works.
My issue is that I want to prioritise related posts that have the most tags in common. Usually I have 3-5 tags for each posts so I want to display posts with 3 tags in common before 1 tag for example. I've tried some different code snippets that should be able to do this but I haven't managed to get it to work so far.
Any help will be appreciated, thanks.
function smak_related_posts() {
global $post;
$tags = wp_get_post_tags( $post->ID );
if($tags) {
foreach( $tags as $tag ) {
$tag_arr .= $tag->slug . ',';
}
$args = array(
'tag' => $tag_arr,
'numberposts' => 4,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'projects'),
);
$related_posts = get_posts( $args );
if($related_posts) {
echo '<footer class="entry-footer">';
echo '<h3>Se også...</h3>';
echo '<div id="masonry-loop">';
get_template_part( 'template-parts/content', 'masonry_sizer' );
foreach ( $related_posts as $post ) : setup_postdata( $post );
get_template_part( 'template-parts/content', 'masonry' );
endforeach; }
}
echo '</div>';
echo '</div>';
wp_reset_postdata();
}
I used a shortcode to include my own customized archive on a WordPress page.
Currently it looks like this:
- Here is title number 1
- Title 2
What I know want to achieve is something like this:
H:
- Here is title number 1
T:
- Title 2
Does somebody know if a function for that already exists somewhere? My code is currently pretty straight forward:
//[show_archive]
function create_archive( $atts ){
$args = array(
'post_type' => 'post',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1,
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
_e("Es sind keine beiträge gefunden worden.");
}
/* Restore original Post Data */
wp_reset_postdata();
}//function
add_shortcode( 'show_archive', 'create_archive' );
Thanks!
while ($the_query->have_posts())
{
$the_query->the_post();
$current_character = ucfirst(substr(get_the_title(),0,1));
echo '<h2>'.$current_character.'</h2>';
if ($last_character != $current)
{
echo '<li><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</li>';
$last_character = $current_character;
}
}
I have two different post types one is books and the other is supports. I am trying to call this two post in one page.
How can I do that? Here is the code I am using to query a post:
<?php query_posts('post_type=books&post_status=publish&posts_per_page=-1&paged='.
get_query_var('paged')); ?>
query_posts should never be used. Rather use WP_Query
$args = array(
'post_type'=>array('books','supports'),
'post_status'=>'publish',
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
<?php
query_posts(
array(
'post_type'=>array('books','supports'),
'post_status'=>'publish',
'posts_per_page'=>-1,
'paged'=>get_query_var('paged')
)
);
?>
I have the following code and need to show THREE things from my custom post type called fact-sheet.
The Title
The summary (fact_sheet_summary)
A file upload URL (fact_sheet_pdf_link)
I can get the first two to work but no idea how to do the third.
Basically my output should be...
The Title
The Summary paragraph
Click here to download as PDF
Any ideas how I can query to list all of these post type results? Is there a better way to do this rather than what I have below? The main problem is, I can't get the URL of the uploaded file.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
));
if($posts)
{
foreach($posts as $post)
{
echo '<span class="fact-sheet-title">' . get_the_title($post->ID) . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . the_field('fact_sheet_summary') . '</span></p>';
}
}
?>
I think It's better to use query_posts() as you can use The Loop default structure.
As for the custom fields (Using ACF Plugin), you're using the_field(), which automatically echoes the retrieved field value. You can use, in other hand, the get_field() function, which just returns the value of the field.
You could do something like this:
// Query all posts from 'fact-sheet' post_type
query_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
// Getting all posts, limitless
'posts_per_page' => -1,
));
// Loop throught them
while(have_posts()){
the_post();
echo '<span class="fact-sheet-title">' . get_the_title() . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . get_field('fact_sheet_summary') . '</span></p>';
// Echos the link to PDF Download
echo '<p>Click here to download as PDF</p>';
}
// Once you're done, you reset the Default WP Query
wp_reset_query();
In case you might need further explanation about wp_reset_query(), check this out.
Can you try this? It's slightly modified from the manual of the WP Codex (not much)
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'post_mime_type' => array('application/pdf'),
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
the_attachment_link( $attachment->ID, true );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
If it works, maybe it's better to modify a working sample to your needs - by adding your custom fields. Just my thoughts :-)