A cone beam computed tomography (CBCT) scanner integrated with a linear accelerator is a powerful tool for image guided radiotherapy. Respiratory motion, however, induces artifacts in CBCT, while the respiratory correlated procedures, developed to reduce motion artifacts in axial and helical CT are not suitable for such CBCT scanners. We have developed an alternative respiratory correlated procedure for CBCT and evaluated its performance. This respiratory correlated CBCT procedure consists of retrospective sorting in projection space, yielding subsets of projections that each corresponds to a certain breathing phase. Subsequently, these subsets are reconstructed into a four-dimensional (4D) CBCT dataset. The breathing signal, required for respiratory correlation, was directly extracted from the 2D projection data, removing the need for an additional respiratory monitor system. Due to the reduced number of projections per phase, the contrast-to-noise ratio in a 4D scan reduced by a factor 2.6-3.7 compared to a 3D scan based on all projections. Projection data of a spherical phantom moving with a 3 and 5 s period with and without simulated breathing irregularities were acquired and reconstructed into 3D and 4D CBCT datasets. The positional deviations of the phantoms center of gravity between 4D CBCT and fluoroscopy were small: 0.13 +/- 0.09 mm for the regular motion and 0.39 +/- 0.24 mm for the irregular motion. Motion artifacts, clearly present in the 3D CBCT datasets, were substantially reduced in the 4D datasets, even in the presence of breathing irregularities, such that the shape of the moving structures could be identified more accurately. Moreover, the 4D CBCT dataset provided information on the 3D trajectory of the moving structures, absent in the 3D data. Considerable breathing irregularities, however, substantially reduces the image quality. Data presented for three different lung cancer patients were in line with the results obtained from the phantom study. In conclusion, we have successfully implemented a respiratory correlated CBCT procedure yielding a 4D dataset. With respiratory correlated CBCT on a linear accelerator, the mean position, trajectory, and shape of a moving tumor can be verified just prior to treatment. Such verification reduces respiration induced geometrical uncertainties, enabling safe delivery of 4D radiotherapy such as gated radiotherapy with small margins.