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 );
Related
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 simply want to embed my woocommerce product details info into my "Classes" page. I can best explain what I want with images, so am including 3.
The Classes page as it is now - http://prnt.sc/c9requ
The product details page - http://prnt.sc/c9rfa7
(the rest is in my comment below)
You can fetch product category from woocommerce by using this code:
$args = array( 'post_type' => 'product', 'category' => 34 );
$products = get_posts( $args );
Once you have the products, you can loop through and display them using your existing HTML code.
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'
It's a bit more complicated than that.
For a travel website, I have 3 parent categories: Series, Post Type, and Location.
Each post is assigned to a child category. For example (respectively): EU2014, Picture Gallery, and Rome.
On each post, there is a sidebar. My intention is that this sidebar will contain links to the other related posts. A post is related if it is:
1.) In the same Series child category (such as "EU2014")
AND
2.) in the same Location child category (let's say "Rome").
Really what I'm doing here is making it so that way the client can write any number of posts, and a network of links will appear on all of them (in the sidebar).
So, in sum, all posts designated "EU2014" AND "Rome" will be part of a collection.
Then I can format the sidebar nicely based on what Post Type a post in question is (with appropriate icons or whatnot).
My problem is this:
I need pseudo code. I've started working on it, but I'm very concerned that, if I am to iterate through the entire database of posts every time I load a post, simply to write that sidebar, it will be a massive resource drain on the system.
How can I
1.) Identify the child categories of the current post
and
2.) get the names and links of each post whose Series and Location child categories match those of the current post in order to write that information to the sidebar of the current post
without creating a black hole of fuckery when the site grows to more than just a few total posts?
This is going to be written in PHP, so if anyone has any interest in helping me figure out more than just pseudo, I'd be thrilled to suss it out with you.
<?php
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => 8, // specify category id here
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
foreach( $myposts as $post )
{
setup_postdata( $post );
?>
<?php the_title(); ?>
<?php
}
wp_reset_postdata();
?>
For Reference see this
http://codex.wordpress.org/Template_Tags/get_posts