ACF and Wordpress extract data from object - wordpress

I have two Custom Post Types
Buildings
Apartments
They are connected thru Post Object. Apartment have field where i choose Building, and i can extract some data like ->
<?php
$ligging = get_field('project_for_apartment');
?>
<?php the_field('location_facility', $ligging->ID) ?>
With that code i extract location from Project page to apartment. That works without any error. But when i try to extract data from repeater field on project page i dont get any results. How can i extract data from fields in repeater (on another page)
Thank you a lot !

The repeater field is usually used in a loop, as its name implies. Have you looked at their doco?
http://www.advancedcustomfields.com/resources/repeater/

The return value of a repeater field (when not used in a loop) is an array, so you could do something like this:
$repeater = get_field('field_name');
foreach($repeater as $field){
echo $field['sub_field_name'];
}

Related

Creating a WordPress website using a Database/Table

I am relatively new to WordPress but have a lot of coding experience so I am hoping to pick it up quite quickly.
I am looking to create a website on WordPress that uses a database.
For example, I have made a table of animals consisting of 100 rows, one for each animal, and this is accessible on phpMyAdmin. I want my website to have a page for each row entry in this database.
Ideally, I would like a page to be shown to the user which contains filters. So the user can select maybe "Small" animals which are "Nocturnal". This will then use the database to find all animals which are "Small" and "Nocturnal", and then the page will display the links to the animals which match these filters. Say only "Hamster" matches these filters in my database, then the link to the Hamster page would display on the website.
Then the user can click the Hamster page to find out more about the animal. So I need a page for each animal, but wondered if I could somehow link this to a database I have to help with the filtering options.
Thanks.
Step 1: Create the animals page and display all of them
Set up a page template to show all your animals first, without filters. That should get you started.
To retrieve them, modify the page template file you created above to fetch all the rows in the animals table, like so:
global $wpdb;
$feeds = $wpdb->get_results("SELECT * FROM animals", ARRAY_A);
Iterate through the rows to print them out into table form.
Step 2: Create the filters
Create the links on the front end using query parameters, e.g. for a link to all nocturnal animals, use something like:
echo 'Nocturnal';
Step 3: Handle a specific query variable
At the top of your animals template page, use the following code to get a query variable:
$animal_type = get_query_var('animal_type');
So if you accessed the URL http://www.example.com/animals/?animal_type=nocturnal then the value of $animal_type would now be nocturnal.
Now create a new function in functions.php to query for animals using query parameters. Something like this:
$args = array('animal_type' => $animal_type, 'animal_size' => $animal_size);
function get_animals($args);
And iterate through them to print them out.

Getting taxanomy value from URL

I'm setting up a search page by views exposed filter. And one of the field are filter by taxonomy term.
For example, when I search with the taxonomy term filter field, the URL is like below.
domain.com/search?subjects=69
Now I wish to get the value of the taxonomy (it's showing tid instead of value)
<?php
$idenity = $_GET['subjects'];
print $idenity;
?>
Anyway to get the value of the taxonomy value but not taxonomy id?
You didn't specify which version you're using so I'm assuming Drupal 7 because it's the latest stable version.
You can load the term with taxonomy_term_load():
<?php
$tid = intval($_GET['subjects']);
$term = taxonomy_term_load($tid)
print $term->name;
?>
Personally, I find that the comments on the Drupal API site are usually very helpful in understanding how to use functions that sound like they are relevant to my problem.

Drupal - Views. Setting a filter programmatically

I hope this is not a stupid question I have been searching for most of the day!
I have a Content Type (Documents) which simply contains a title, file and a category. The category value is required and is 'powered' by Taxonomy.
I now wish to create a view which will display these documents grouped and titled by the taxonomy term.
Using my limited Drupal knowledge I intent to iterate through the relevant terms IDs (using taxonomy_get_tree($vid)) and then render each view accordingly.
To do this I have been hoping to use this snippet.
view = views_get_view('documents');
$view->set_display($display_id);
$filter = $view->get_item($display_id, 'filter', 'field_dl_category');
$filter['value']['value'] = $filter_value;
$view->set_item($display_id, 'filter', 'field_dl_category', $filter);
$viewsoutput = $view->render();
But this is not working; when I query the value of the $filter ($view->get_item($display_id, 'filter', 'field_dl_category')) I get null returned.
Might this be that my filter name is not the same as the CCK field name?
I am using Drupal 7.
Any help much appreciated, I am running out of ideas (and time).
I finally managed to get this working but I took a slightly different approach.
I changed my view and added the relevant contextual filter and then used this function views_embed_view to get at my required results.
If this helps! this is my solution:
$display_id = 'default';
$vid = 7;
$terms = taxonomy_get_tree($vid);
foreach($terms As $term){
$content = views_embed_view('documents', $display_id, $term->tid);
//now we see if any content has been provided
if(trim($content) != ''){
print "<h3>" . $term->name . "</h3>";
print $content;
}
}
In my case the trim($content) returns '' with no data as the view template has been edited, this might not be the case for all.
I am a very new Drupal developer so I'm sure there are much better ways of doing this, if so please do post.
I am going to go ahead and assume that you want to show, using Views, a list of document nodes grouped by the category that they have been tagged with.
There are two (of maybe more) ways by which you can do this in Views 3:
(a) Choose a display style that allows you to select a Grouping field. (You could try the table style that ships with Views by default). Suppose you have properly related the node table to the taxonomy_term_data table through a Views relationship, you could choose taxonomy_term_data.name as the grouping field.
Note that this grouping is done before the view is just rendered. So, your query would just have to select a flat list of (content, tag) pairs.
(b) You could also make use of the Attachment display type to achieve something similar. Show the used categories first in a list view clicking on which will show a page (attachment) with all documents tagged in that chosen category.
To understand how to do (a) or (b), turn on the advanced_help module (which is not a Views requisite but is recommended) first.
For (a), read the section on Grouping in styles i.e. views/help/style-grouping.html and
For (b), read the section on Attachment display i.e. views/help/display-attachment.html
A couple of things about your approach:
(a) It will show all terms from that vocabulary irrespective of whether or not they were used to tag at least one document.
(b) views_embed_view() will return NULL even if the currently viewing user does not have access to the view. So, ensure that you catch that case.
Here's an alternative:
$view = views_get_view('view_machine_name');
$view->init_display('default');
$view->display_handler->display->display_options['filters']['your_filter_name']['default_value'] = 'your_value';
$view->is_cacheable = FALSE;
$view->execute();
print $view->render();
I know you can probably set this using some convoluted method and obviously that would be better. But if you just want a quick and dirty straight access without messing around this will get you there.

Use Wordpress custom post in a contact form

I have created custom post type "Product" in Wordpress and I would like to use Products within my contact form. For example, I would like to have a drop down that is a list of all of my Products so users can select a Product name as the message's Subject. I have Contact Form 7 installed. Is there an easy way to do this?
Thanks !
I think the short answer is no. There is not an easy way to do this. The Contact Form 7 plugin uses shortcodes to construct the select lists. What you need to do is run a query on your Posts -> Products and generate your own select list. I suppose what I would do is write my own shortcode function. Then you can include it in your page.
[myProductsShortCode]
Then you can iterate through that result set and generate your own select list.
http://codex.wordpress.org/Shortcode_API
http://codex.wordpress.org/wpdb#query_-_Run_Any_Query_on_the_Database
People seem to be able to add custom information like that, from
function test_generator() {
/* need to produce html like this:
<span class="wpcf7-form-control-wrap menu-645"><select name="menu-645" class="wpcf7-select"><option value="one">one</option><option value="two">two</option></select></span>
so here we go: */
$list = "<span class=\"wpcf7-form-control-wrap menu-test\"><select name=\"menu-test\" class=\"wpcf7-select\"><option value=\"test1\">test-1</option><option value=\"test2\">test-2</option></select></span>";
return $list;
}
wpcf7_add_shortcode('test', 'test_generator');
and then just use [test] in the contactform

Wordpress Archive by Year "query_posts()" Problem

I created an Archive list (also this is my archive.php codes) on my blog and i want to show posts by years.
But when i use query_posts() function for excluding some categories and limit posts then it's showing all posts not by year.
For example this page is showing all posts not only posts in 2009 years.
So if i summarize this issue; i want to show archive list by year (when i enter /2009/ permalink)+exclude some categories and limit posts.
Thank you.
maybe when you use global variable, $query_string, it will help.
so you must use it like this
global $query_string;
query_posts($query_string . '&cat=-13,-4,-14,-171&posts_per_page=5&paged='. $paged);
as codex mention, here : query post,
Place a call to query_posts() in one of your Template files before The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

Resources