Wordpress ACF -> querying page templates instead of posts.
So basically I have setup advanced custom fields and I want to structure my website so that when I create a page with a page template of, say, 'topic-page' I would like ACF to add the custom fields to all pages that have a page template of 'topic-page'.
In the past I would use the $args array to query the post_type = 'slug'; however, how do I query a page template?
Existing code for querying post types:
$args = array( 'post_type' = 'the name of the post type' ); $args = new WP_Query( $args );
So I would like to modify it to be able to reference fields from a page template.
Thanks.
To query pages you can set the post_type to 'page':
'post_type' => 'page'
Related
In my WordPress v5.7, currently I have two custom post_type: song, & poem.
I have a custom author.php template with author profile and latest 10 posts from both post_type with no pagination, as required by site design. Here is the author template at pastebin (https://pastebin.com/kCrYebcD).
If the author has more than 10 posts from both post_type, I want to show all posts from both post_type in archive.php template with pagination. Here is the archive template at pastebin (https://pastebin.com/twkWn5Bc).
In author.php I have this URL to redirect to all post archive page:
<?php
$author_page_link = esc_url(get_author_posts_url(false, ID->user_nicename));
echo 'All';
?>
This is how the URL constructs: http://www.local.site/author/one/?post_type[]=song&post_type[]=poem.
The above URL is redirecting back to author.php page. If I use only any one post type as below, the URL stays in the archive.php.
All
How can I show a single author's all posts from selective custom post_type in the archive.php?
This is how I have solved:
I have created a custom page template author-all-posts.php with page slug all. I have used the custom URL parameters as below:
http://www.local.site/all/?author_id=1
In my custom page template, I have the below code:
// get the author ID
$authorID = $_GET['author_id'];
Below the WP_Query to get all the posts from multiple post_types:
$all_args = array(
'author' => $authorID,
'post_type' => array('song', 'poem'),
'posts_per_page' => -1
);
query_posts($all_args);
I am working on add_rewrite_rule to red rid of the ugly URLs next.
In the folder of mu-plugin I created a file, then created a function to query all posts with type "speaker", but I don't know, how to put them into a select box named "Speakers" which created with custom-field WordPress plugin.
This the function that I query all posts with type speaker:
function displayPosts()
{
$homepageSpeaker = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'speaker'
));
while($homepageSpeaker->have_posts()) {
$homepageSpeaker->the_post();
// Add the id and name of each post in a select box named "Speakers"
}
}
add_action('admin_menu','displayPosts');
Where do you want to have that select box?
A mu-plugin does not sound like the right place for that.
What plugin is the "custom-field WordPress plugin" you are using?
Likely they have a documentation how to add options to certain select fields.
I am working on a website in Wordpress where I need to use custom posts (which I already created with the help of a plugin).
The problem is that the theme that I use allows me to display the post on the page organized according to categories, but when I create a custom post and put it into a category it is not displayed on the web (as if I had never created the post) but if I create the same post from the normal page of Wordpress entries (a standard Wordpress post type) and I put it in the same category this is shown on the page. Also, when I enter the custom post page the entry I created appears but when I enter the normal entries page it does not appear.
I went to a portal where they said how to add the custom post to the Wordpress categories by writing some lines of code in the functions.php file but this did not work, now I see the custom post within the category page but I still do not see them inside of the Wordpress entries page and also still not shown on the web.
You need to create a custom query. This page has good explanations and examples: https://codex.wordpress.org/Class_Reference/WP_Query
The most important thing in your case is to include this in your arguments array, which selects posts and your CPT:
'post_type' => array('post', 'your-custom-posttype'),
and also this which filters by category:
'category_name' => 'your_category_name'
So a typical simple custom query would look like this:
$args = array(
'post_type' => array('post', 'your-custom-posttype'),
'category_name' => 'your_category_name',
'post_status' => 'publish',
'posts_per_page' => 12
);
$query1 = new WP_Query( $args );
if ( $query1->have_posts() ) {
while ( $query1->have_posts() ) {
$query1->the_post();
echo '<li>' . get_the_title() . '</li>';
// Other stuff echoing content etc. to be added here.....
}
wp_reset_postdata();
}
I'm building a custom plugin for WordPress to display some custom posts.
So far so good, it worked (I had to re-save the permalinks), and now I'm able to see it in my archive page and, since I added the slug in the array for the register_post_type function, I can see them adding the slug in the url (for example: mywebsite.com/coolposts:
'rewrite' => array( 'slug' => 'coolposts' ),
So far so good but I have a problem and I don't know how to solve it.
I want to display my custom posts "coolposts" in my custom query:
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50');
while ($the_query->have_posts()) :
$the_query->the_post();
if ((($the_query->current_post+1) % 3 == 0) && ($the_query->current_post+1 !== count($the_query->posts))):
echo "</div><div class='row'>";
endif;
endwhile;
Because, for some reason, my custom posts are excluded from the main query.
Am I doing something wrong?
Just to be clear, I don't want to display ONLY my customs posts, I want to display all of my posts.
I assume you registered custom post type with the name coolposts when registered with register_post_type function. If so then just add another param post_type=coolposts. To avoid unexpected situation you can add one more param post_status=publish, this param will ensure only published posts are displayed.
$the_query = new WP_Query('orderby=title&order=asc&posts_per_page=50&post_type=coolposts');
I am making an e-commerce site with WordPress, Woocommerce, and BuddyPress. We allow members to upload images of their products. The product category name is the same as the member's username. So if exampleUser1 uploads a product image, that product is given the category 'exampleUser1'.
This is a temporary method that my team and I are using so members can see all of their products on one page (product-category page).
I want to create a dynamic URL that I can display via a button that links to the product-category page of the logged in user. I have researched how to do this using functions.php and passing variables to the URL but am having no luck getting anything to work.
Thank you for your help!
You should change the category to a constant value for all users like productImage or something. Then no need for a dynamic URL, the same URL will work for all users. Just create a page template and use WP_Query in the loop like this:
$author = get_current_user_id();
$args = array(
'author' => $author,
'post_status' => 'any',
'category_name' => 'productImage',
'post_type' => 'attachment'
);
$query = new WP_Query( $args );